def main(): celery_val_init = add.delay(10, 20) standby(celery_val_init) celery_val_init = mul.delay(randint(0, sys.maxsize), randint(0, sys.maxsize)) standby(celery_val_init) celery_val_init = add.delay('ab', 'ba') standby(celery_val_init) celery_val_init = mul.delay(randint(0, sys.maxsize), randint(0, sys.maxsize)) standby(celery_val_init)
''' celery第二个例子,展示如何的应用中使用celery celery -A proj worker -l info ''' #--------------------------以下是test---------------------------- from proj.tasks import add print(add(3,4)) res = add.delay(2, 2) print(res.get(timeout=1)) s1 = add.subtask((2, 2), countdown=10) res = s1.delay() res.get() # add options s3 = add.s(2, 2, debug=True) s3.delay(debug=False) # debug is now False. # Groups group(add.s(i, i) for i in xrange(10))().get() # Partial group g = group(add.s(i) for i in xrange(10)) g(10).get() # Chains (4 + 4) * 8 chain(add.s(4, 4) | mul.s(8))().get()
from proj.tasks import add import time t1 = time.time() r1 = add.delay(1, 2) print('1') r2 = add.delay(2, 4) print(2) r3 = add.delay(3, 6) r4 = add.delay(4, 8) r5 = add.delay(5, 10) r_list = [r1, r2, r3, r4, r5] print('6') for r in r_list: while not r.ready(): pass print(r.result) t2 = time.time() print('共耗时:%s' % str(t2 - t1))
from proj.tasks import add, mul, xsum, long_sum from celery import group from celery import chain from celery import chord import time result = add.delay(4, 3) print(type(result)) # result = add.apply_async((2, 2)) # print(type(result)) # print(add(2, 2)) print('@@@@@@@@@@@@@@@@@@@@@@@@') print(result.failed()) print(result.id) print(result.backend) print(result.collect()) print(result.ready()) print(result.get(propagate=False)) # print(result.ready()) print(result.failed()) print('@@@@@@@@@@@@@@@@@@@@@@@@') func = add.s(2, 2) print(type(func)) print(func) res = func.delay() print(res.get())
# coding:utf-8 from proj.tasks import add for i in range(1000000): r = add.delay(6)
#!/usr/bin/env python import sys sys.path.append("./proj") import time from proj.tasks import add x = 4 y = 5 print "Calling add(%s, %s)" % (x, y) result = add.delay(x, y) while not result.ready(): print "Sleeping...waiting for result" time.sleep(1) print "Task yield result of: %s" % (result.result)
def do_it(): user = request.oauth.user.username add.delay(user) return json.dumps({"status": "sending"})
import random from celery import group, chain from proj.tasks import add, mul, xsum ############ # Single CPU ############ ans_pool = [] for i in range(0, 10): rand1 = random.randint(0,99) rand2 = random.randint(0,99) ans_pool.append( (add.delay(rand1, rand2).get(), mul.delay(rand1, rand2).get()) ) print(ans_pool) ############## # Multiple CPU + Group/Chord ############## # Prepare data and assign tasks prepare_add = [] prepare_mul = [] for i in range(0, 10): rand1 = random.randint(0, 99) rand2 = random.randint(0, 99) prepare_add.append(add.s(rand1, rand2)) prepare_mul.append(mul.s(rand1, rand2))
from celery import group, chain, chord from proj.tasks import add, mul, xsum if __name__ == '__main__': # (i + i) for i in range 100 for i in range(100): result = add.delay(i, i) print(result.get(propagate=False)) # signatures # (8 + 2) s1 = add.s(2) res = s1.delay(8) print(res.get()) # groups # list of (10 + i) for i in range 10 g = group(add.s(i) for i in range(10)) print(g(10).get()) # chains # (? + 4) * 8 c = chain(add.s(4) | mul.s(8)) print(c(4).get()) # chord # sum of all i + i in range 10 m = chord((add.s(i, i) for i in range(10)), xsum.s())().get() print(m)
def test_add(): result = add.delay(4, 4) print result.get(timeout=1)
from proj.tasks import add, div, sub from proj.celery import app from celery import group '''for show state app''' res = app.AsyncResult( '16d5d64a-28ed-4b6a-97e0-5787b2f335fe') #this id of output run tasks print(res.successful()) print(res.failed()) print(res.state) print(res.id) func = add.signature( (2, 18)) #(2, 18) default for task add this add to the end task #add.s(18) this s not accept to parameter res = func.delay() # this add to the end res.forget() rs = add.delay(5, 2) print(rs.get()) rs = div.delay(63, 3) print(rs.get()) result = sub.apply_async((5, 6), queue='celery', countdown=3) print(result.get()) ''' add.apply_async(args=[5,6], countdown=10) #10 secound delay fro run and asyncrinize change to syncrinize print(rs.get(timeout=2)) #if more than of 2 secound not response then leave it print(rs.get(propagate=False)) #if eror was you leave and continue run code '''
path = 'C:\MyProjects\projCelery\proj01' import sys sys.path.append(path) from proj.tasks import add r = add.delay(4, 4) print(r.status) print(r.result)
def test_add(): res = add.delay(4, 4).get(timeout=10) assert res == 8
from proj.tasks import add import time t1 = time.time() r1 = add.delay(1, 2) r2 = add.delay(2, 4) r3 = add.delay(3, 6) r4 = add.delay(4, 8) r5 = add.delay(5, 10) r6 = add.delay(11, 12) r7 = add.delay(12, 14) r8 = add.delay(13, 16) r9 = add.delay(14, 18) r10 = add.delay(15, 20) r_list = [r1, r2, r3, r4, r5, r6, r7, r8, r9, r10] for r in r_list: while not r.ready(): pass print(r.result) t2 = time.time() print(t2 - t1)
from proj.tasks import add, div, sub, avg rs = add.delay(34, 4) print(rs.get()) rs = sub.delay(34, 4) print(rs.get()) rs = div.delay(34, 4) print(rs.get()) ''' celery -A proj worker -Q qa,qs,qd,qv -l info --->create queue celery -A proj inspect active --->show tasks active in now celery -A proj inspect active --help celery -A proj events --dump --->monitoring celery -A proj control enable_events celery -A proj control disable_events '''
from proj.tasks import add range = pow(10, 3) for i in xrange(range): if i % 1000 == 0: print 'publishing: ', i add.delay(0, i) print 'done!'
from proj.tasks import add, mul, xsum import time if __name__ == '__main__': tasks = [add.delay(1, 2), mul.delay(1, 2), xsum.delay([1, 2, 3, 4, 5])] for t in tasks: while not t.ready(): time.sleep(1) msg = "id: {}, name: {}, result: {}".format(t.id, t.name, t.result) print(msg)
from proj.tasks import add from proj.celery import app id = add.delay(2, '2').id print("ID:", type(id), id) res = app.AsyncResult(id) print("RESULT:", res.get(timeout=1, propagate=False)) print("STATE:", res.state) print("TRACEBACK ------") print(res.traceback)
def ping(): what.delay() add.delay(3, 3) mul.delay(12345678, 987654321) return jsonify(msg='pong')
from __future__ import absolute_import # Start a Celery worker by executing: # celery -A proj worker -l info # Import available tasks from proj.tasks import add, mul, xsum, fib # Test short-running tasks add.delay(2, 2) mul.delay(10, 12) xsum.delay(range(100)) fib.delay(10) # Test medium-running tasks fib.delay(35) fib.delay(35) fib.delay(35)