myapp.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 celery import Celery
  20. app = Celery(
  21. # XXX The below 'myapp' is the name of this module, for generating
  22. # task names when executed as __main__.
  23. 'myapp',
  24. broker='amqp://guest@localhost//',
  25. # ## add result backend here if needed.
  26. # backend='rpc'
  27. )
  28. app.conf.timezone = 'UTC'
  29. @app.task
  30. def say(what):
  31. print(what)
  32. @app.on_after_configure.connect
  33. def setup_periodic_tasks(sender, **kwargs):
  34. # Calls say('hello') every 10 seconds.
  35. sender.add_periodic_task(10.0, say.s('hello'), name='add every 10')
  36. # See periodic tasks user guide for more examples:
  37. # http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html
  38. if __name__ == '__main__':
  39. app.start()