version:
(celery==4.3,rabbitmq==3.7)
A. Use rabbitmq
# Mounting RabbitMQ (MAC) BREW the install RabbitMQ # configure the environment variables (.bash_profile or .profile, note the path) Export RABBIT_HOME = / usr / local / Cellar / RabbitMQ / 3.7.15 Export the PATH = the PATH $: $ RABBIT_HOME / sbin # Set RabbitMQ: create a user, a web hosting and set permissions (need to start the service) sudo rabbitmqctl add_user myuser mypassword sudo rabbitmqctl add_vhost myvhost # virtual host sudo rabbitmqctl set_user_tags myuser MyTag by a value # Administrator sudo rabbitmqctl set_permissions -p myvhost myuser " *. " " * " " * " # start? service sudo rabbitmq-Server # running in the background sudo RabbitMQ-Server - detached # Do not kill (1) to stop the server sudo rabbitmqctl stop
Use two .celery
# Install
pip install celery
application
# Tasks.py from Celery Import Celery App = Celery ( ' Tasks ' , # current module name Broker = ' AMQP: Guest @ // localhost: // Port ' # message queue url ) @ app.task DEF the Add (X , Y): return X + Y
Run worker
celery -A tasks worker --loglevel=info
Calling task
# delay from tasks import add add.delay(arg1,arg2,kwarg1='x',kwarg2='y') add.delay(*args, **kwargs).apply_async(args, kwargs) # apply_async task.apply_async(args=[arg1,arg2], kwargs={'kwargs':'x','kwargs':'y'}) tasks.apply_async((arg,), {'kwarg': value}) # 从现在起10秒内执行 tasks.apply_async (COUNTDOWN = 10 ) # performed within 10 seconds from now, the specified ETA tasks.apply_async (ETA = now + timedelta (= 10 seconds The )) # performed after one minute from now, but expires after 2 minutes tasks.apply_async (COUNTDOWN = 60, expires = 120 ) # expiration 2 days provided the use datetime objects T.apply_async (expires = now + timedelta (days = 2 )) # send_task: task is not registered in the current process app. send_task ( ' task ' , args = [Arg,], = Queue ' default ' ) # signature to a subject (e.g., transmission via Internet) calls the signature transfer tasks, and they also support API calling task.s (arg1, arg2, = kwarg1 ' X ' , kwargs2 = ' Y').apply_async()