Getting ready

To install Celery, use the pip installer as follows:

C:>pip install celery

Then, a message broker must be installed. There are several choices available, but for our example, it is recommended to install RabbitMQ from the following link: http://www.rabbitmq.com/download.html.

RabbitMQ is message-oriented middleware that implements the Advanced Message Queuing Protocol (AMQP). The RabbitMQ server is written in the Erlang programming language, so in order to install it, you need to install Erlang after downloading it from http://www.erlang.org/download.html.
The steps involved are as follows:
  1. To check the celery installation, first start the message broker (for example, RabbitMQ). Then, type the following:
C:>celery --version
  1. The following output, which indicates the celery version, is as follows:
4.2.2 (Windowlicker)

Next, let's learn about how to create and call a task using the celery module.

celery provides the following two methods to perform a call to a task:

  • apply_async(args[, kwargs[, ...]]): This sends a task message.
  • delay(*args, **kwargs): This is a shortcut to send a task message, but it does not support execution options.
The delay method is easier to use because it is called as a regular functiontask.delay(arg1, arg2, kwarg1='x', kwarg2='y'). Howeverfor apply_asyncthe syntax is task.apply_async (args=[arg1,arg2] kwargs={'kwarg1':'x','kwarg2': 'y'}).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset