remote-tasks.rst 1.7 KB

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