-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_tasks.py
47 lines (40 loc) · 1.02 KB
/
example_tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os
import time
import random
from celery import Celery
queue = 'amqp://{}:{}@{}//'.format(
os.getenv('MQ_USER', 'example'),
os.getenv('MQ_PASS', 'publicsecret'),
os.getenv('MQ_HOST', 'mq'),
)
results = 'db+postgresql://{}:{}@{}/{}'.format(
os.getenv('PG_USER', 'example'),
os.getenv('PG_PASS', 'publicsecret'),
os.getenv('PG_HOST', 'db'),
os.getenv('PG_DB', 'pantry'),
)
app = Celery('example_tasks', backend=results, broker=queue)
app.conf.result_expires = None
app.conf.worker_prefetch_multiplier = 1
app.conf.beat_schedule = {
'add-every-5-seconds': {
'task': 'example_tasks.add',
'schedule': 5.0,
'args': (2, 3)
},
'mul-every-4-seconds': {
'task': 'example_tasks.mul',
'schedule': 4.0,
'args': (3, 4)
},
}
@app.task
def add(a, b):
print("Executing 'add' task")
time.sleep(random.randint(1, 7))
return a + b
@app.task
def mul(a, b):
print("Executing 'mul' task")
time.sleep(random.randint(1, 7))
return a * b