remote-tasks.rst 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. ==============
  2. Remote Tasks
  3. ==============
  4. .. module:: celery.task.rest
  5. Executing tasks on a remote web server
  6. --------------------------------------
  7. If you need to call into another language, framework or similar, you can
  8. do so by using HTTP tasks.
  9. The HTTP tasks (or REST task) uses a simple REST+JSON scheme to take arguments
  10. and return results, the scheme to call a task is::
  11. GET http://example.com/mytask/?arg1=a,arg2=b,arg3=c
  12. The web page should then return a response in the following format
  13. if the execution was successful::
  14. {"status": "success", "retval": ....}
  15. or in the following format if there was an error::
  16. {"status": "failure": "reason": "Invalid moon alignment."}
  17. With this information we can define a simple task in Django:
  18. .. code-block:: python
  19. from django.http import HttpResponse
  20. from anyjson import serialize
  21. def multiply(request):
  22. x = int(request.GET["x"])
  23. y = int(request.GET["y"])
  24. result = x * y
  25. response = {"status": "success", "retval": result}
  26. return HttpResponse(serialize(response), mimetype="application/json")
  27. I'm sure you'll be able to port this scheme to any language/framework.
  28. New examples and libraries are very welcome!
  29. To execute the task you use :class:`RESTProxyTask`:
  30. >>> from celery.task import RESTProxyTask
  31. >>> res = RESTProxyTask.delay("http://example.com/multiply", x=10, y=10)
  32. >>> res.get()
  33. 100
  34. In your ``celeryd.log`` file you should see the task being processed::
  35. [INFO/MainProcess] Task celery.task.rest.RESTProxyTask
  36. [f2cc8efc-2a14-40cd-85ad-f1c77c94beeb] processed: 100
  37. Since applying tasks can also simply be done via the web and the
  38. ``celery.views.apply`` view, executing tasks from other languages should be a
  39. no-brainer.