Debian Codegraph @

examples/periodic-tasks/myapp.py

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