-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcelery
66 lines (34 loc) · 1.97 KB
/
celery
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
48
49
50
51
52
53
54
55
56
57
#to stop workers, allowing complete its current task before existing
ps auxww | grep 'celery worker' | awk '{print $2}' | xargs kill
#to shutdown
ps auxww | grep 'celery worker' | awk '{print $2}' | xargs kill -9
#create a instance
from celery import Celery
app = Celery('tasks', backend='amqp', broker='amqp://')
#'tasks' name to identify our tasks, it will be prepended
#backend is the results go, to query the status of background task or retrieve its results. If without results this is empty paramenter
#broker... the url parameter to connect our broker, the rabbitmq
#Build celery tasks
@app.task # this allows celery to identify funcitons that it can add its queuing functions to. After the decorator , we simply create a function that our workers can run
or
@app.task(ignore_result=True) # to ignore results and say celery dont use the backend to store state information about this task
#now to start a worker processes that will be able to accept connections from apps. It will use the file we just created
celery worker -A tasks &
#this will start up a app in background
#if you want multiple workers, you can do so by naming each one with the -n argument
celery worker -A tasks -n one.%h &
celery worker -A tasks -n two.%h &
# %h replace the hostname when the worker is named
#now to use our celery workers we need use in our python apps the .delay method
primes = gen_prime.delay(50000)
#Celery wraps our functions with additional capabilities.
#This task is now being executed by the workers we started earlier.
#Because we configured a backend parameter for our application, we can check the status of the computation and get access to the result.
#to check
primes.ready()
False
#When False it means that the task is running and no result yet. When we get True.
#When results is ready we can use .get method, for example
print primes.get()
or using with timeout will raise an exception if it times out, which you can handle in your program
print primes.get(timeout=2)