myapp.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """myapp.py
  2. Usage::
  3. # The worker service reacts to messages by executing tasks.
  4. (window1)$ python myapp.py worker -l info
  5. # The beat service sends messages at scheduled intervals.
  6. (window2)$ python myapp.py beat -l info
  7. # XXX To diagnose problems use -l debug:
  8. (window2)$ python myapp.py beat -l debug
  9. # XXX XXX To diagnose calculated runtimes use C_REMDEBUG envvar:
  10. (window2) $ C_REMDEBUG=1 python myapp.py beat -l debug
  11. You can also specify the app to use with the `celery` command,
  12. using the `-A` / `--app` option::
  13. $ celery -A myapp worker -l info
  14. With the `-A myproj` argument the program will search for an app
  15. instance in the module ``myproj``. You can also specify an explicit
  16. name using the fully qualified form::
  17. $ celery -A myapp:app worker -l info
  18. """
  19. from __future__ import absolute_import, unicode_literals, print_function
  20. from celery import Celery
  21. app = Celery(
  22. # XXX The below 'myapp' is the name of this module, for generating
  23. # task names when executed as __main__.
  24. 'myapp',
  25. broker='amqp://guest@localhost//',
  26. # ## add result backend here if needed.
  27. # backend='rpc'
  28. )
  29. app.conf.timezone = 'UTC'
  30. @app.task
  31. def say(what):
  32. print(what)
  33. @app.on_after_configure.connect
  34. def setup_periodic_tasks(sender, **kwargs):
  35. # Calls say('hello') every 10 seconds.
  36. sender.add_periodic_task(10.0, say.s('hello'), name='add every 10')
  37. # See periodic tasks user guide for more examples:
  38. # http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html
  39. if __name__ == '__main__':
  40. app.start()