title: Celery - Asynchronous Task Chaining
body: <p>I am trying to develop a proof-of-concept to for asynchronous task chaining on using celery with redis as broker.</p>
  
  <p>The program is Flask API with /run and three functions that need to run as async tasks such that return from a(), is argument of b() and return b() is argument for c() which writes the data into a mongodb collection through a collection object 'collection'.</p>
  
  <pre><code>@celery.task
  def a(param):
      print("Original: {0}".format(param))
      print("Inside Task 1")
      param.update({"timestamp_A":str(datetime.timestamp), "result_A":True})
      print(param)
      return param
  
  @celery.task
  def b(param):
      print("Inside Task 2")
      param.update({"timestamp_B":str(datetime.timestamp), "result_B":True})
      print(param)
      return param
  
  @celery.task
  def c(param):
      print("Inside Task 3")
      collection.insert(dict(param))
      print("Output Saved to DB")
  
  
  @app.route('/run', methods = ['GET'])
  def run():
      if request.method != 'GET':
          return "HTTP Method not allowed"
  
      if request.method == 'GET':
          T = 1000
          for num in range(0, T):
              ds = {"test": num}
              chain(a.s(ds) | b.s() | c.s()).apply_async()
          return "Process Complete"
  
  if __name__ == '__main__':
      app.run(debug=True)
  </code></pre>
  
  <p>With the above code task chaining works i.e. a() gets executed with with its parameter, but for function b() to get executed it waited for the entire data to be queued in a() first and only then did it execute b(). I need that soon as any task a() gets executed it should be handed to b() and so on..
  Anyone has any pointers on where I might be going wrong?</p>