task.py 982 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import time
  2. import logging
  3. import random
  4. logging.basicConfig(level=logging.DEBUG,
  5. format='%(asctime)s %(message)s',
  6. datefmt='%H:%M:%S')
  7. from celery import Celery
  8. celery = Celery('hello', backend='redis://localhost',
  9. broker='redis://localhost:6379/0')
  10. @celery.task(name='create_continous')
  11. def create():
  12. sec = random.uniform(2, 5)
  13. time.sleep(sec)
  14. logging.info('hello world')
  15. return 'slept! %.2f' % sec
  16. if __name__ == '__main__':
  17. celery.control.broadcast('pool_restart',
  18. arguments={'reload': True})
  19. res = []
  20. N = 14
  21. def callback(*args, **kwargs):
  22. print(args)
  23. print(kwargs)
  24. print('---')
  25. for i in xrange(N):
  26. print('append', i)
  27. res.append(create.apply_async(expires=2))
  28. for i in xrange(N):
  29. logging.info('wait %d' % i)
  30. val = res[i].get()
  31. logging.info('got %s' % str(val))
  32. time.sleep(30)