CELERY PYTHON HOW TO CREATE YOUR FIRST CELERY TASK

TABLE OF CONTENTS

Introduction

Celery is an open-source distributed task queue with a focus on real-time operations but also has support for task scheduling. A task queue distributes work across several machines or threads. To get a better understanding of celery lets first understand a few terms used in celery.

Important terminologies

To understand how celery works, we are going to look at a few terms used in celery.

Task – Is a single unit of work that needs to be accomplished.

Client / Producer – An application that needs a task to be done. It achieves this by sending out messages.

Worker/Consumers – This is the actual server application that does the task processing. They consume and execute tasks.

Broker – Transports and distributes tasks to the workers. It acts as the middleman between tasks and workers. Example of message brokers are redis broker and rabbitMQ which is the default broker for celery and is written using Erlang language.

A celery system can consist of several workers and brokers which yields to high availability and enables horizontal scaling.

To visualize this, the following is the architecture of Celery.

Celery architecture
Celery

What are celery use cases?

The following are the most common use cases for celery.

  • Scheduling work that runs periodically, eg sending emails at the end of the day.
  • Where there is need for work to run in the background. For example updating a user’s web page in the background.
  • Performing asynchronous execution with retries to make sure a particular job id done.
  • Running a task after a web request has completed.

Prerequisites

To start using celery you need to install a message broker which will be used to send and receive messages. As stated earlier there are many other message brokers such as redis broker, Amazon SQS and Zookeeper.

In this tutorial we are going to use RabbitMQ which servers as the default broker for celery, is open source and uses the AMQP protocol(Advanced Message Queuing Protocol).

Installing and configuring RabbitMQ

When using rabbitMQ with celery you don’t need to install any dependencies since or perform any initial configurations, you will only have to provide the URL location of the broker instance you want to use. Check this link for a full list of broker settings available with celery

broker_url = 'amqp://user:password@localhost:5672/virtual_host'

You can use brew to install rabbitMQ on macOS

brew install rabbitmq

To install rabbitMQ on ubuntu and Debian systems, we will use apt repositories in cloudsmith. Copy the following bash script and execute it from the terminal

#!/usr/bin/sh

sudo apt-get install curl gnupg apt-transport-https -y

## Team RabbitMQ's main signing key
curl -1sLf "https://keys.openpgp.org/vks/v1/by-fingerprint/0A9AF2115F4687BD29803A206B73A36E6026DFCA" | sudo gpg --dearmor | sudo tee /usr/share/keyrings/com.rabbitmq.team.gpg > /dev/null
## Cloudsmith: modern Erlang repository
curl -1sLf https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/gpg.E495BB49CC4BBE5B.key | sudo gpg --dearmor | sudo tee /usr/share/keyrings/io.cloudsmith.rabbitmq.E495BB49CC4BBE5B.gpg > /dev/null
## Cloudsmith: RabbitMQ repository
curl -1sLf https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/gpg.9F4587F226208342.key | sudo gpg --dearmor | sudo tee /usr/share/keyrings/io.cloudsmith.rabbitmq.9F4587F226208342.gpg > /dev/null

## Add apt repositories maintained by Team RabbitMQ
sudo tee /etc/apt/sources.list.d/rabbitmq.list <<EOF
## Provides modern Erlang/OTP releases
##
deb [signed-by=/usr/share/keyrings/io.cloudsmith.rabbitmq.E495BB49CC4BBE5B.gpg] https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/deb/ubuntu bionic main
deb-src [signed-by=/usr/share/keyrings/io.cloudsmith.rabbitmq.E495BB49CC4BBE5B.gpg] https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/deb/ubuntu bionic main

## Provides RabbitMQ
##
deb [signed-by=/usr/share/keyrings/io.cloudsmith.rabbitmq.9F4587F226208342.gpg] https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/deb/ubuntu bionic main
deb-src [signed-by=/usr/share/keyrings/io.cloudsmith.rabbitmq.9F4587F226208342.gpg] https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/deb/ubuntu bionic main
EOF

## Update package indices
sudo apt-get update -y

## Install Erlang packages
sudo apt-get install -y erlang-base \
                        erlang-asn1 erlang-crypto erlang-eldap erlang-ftp erlang-inets \
                        erlang-mnesia erlang-os-mon erlang-parsetools erlang-public-key \
                        erlang-runtime-tools erlang-snmp erlang-ssl \
                        erlang-syntax-tools erlang-tftp erlang-tools erlang-xmerl

## Install rabbitmq-server and its dependencies
sudo apt-get install rabbitmq-server -y --fix-missing

The following is sample output Assuming you saved the file in a script called rabbitmqinstall

$./rabbitmqinstall
Reading package lists... Done
Building dependency tree       
Reading state information... Done
curl is already the newest version (7.68.0-1ubuntu2.7).
gnupg is already the newest version (2.2.19-3ubuntu2.1).
apt-transport-https is already the newest version (2.0.6).
The following packages were automatically installed and are no longer required:
  augeas-lenses cpu-checker db-util db5.3-util dctrl-tools dkms dmeventd
  erlang-asn1 erlang-eldap erlang-ftp erlang-inets erlang-os-mon
  erlang-parsetools erlang-public-key erlang-snmp erlang-ssl erlang-tftp
  erlang-xmerl extlinux gconf-service gconf-service-backend gconf2-common
  hfsplus ibverbs-providers icoutils ipxe-qemu ipxe-qemu-256k-compat-efi-roms
  kbuild kpartx ldmtool libafflib0v5 libarchive-tools libaugeas0 libcacard0
  libconfig9 libdate-manip-perl libdevmapper-event1.02.1 libewf2 libfdt1
  libgconf-2-4 libgl2ps1.4 libgsoap-2.8.91 libguestfs-hfsplus libguestfs-perl
  libguestfs-reiserfs libguestfs-tools libguestfs-xfs libguestfs0 libhfsp0
  libhivex0 libibverbs1 libintl-perl libintl-xs-perl libiscsi7
  libjs-regenerate libjs-source-map libjs-sprintf-js libldm-1.0-0 libllvm11
  liblvm2cmd2.03 liblzf1 liboce-foundation11 liboce-modeling11
  liboce-ocaf-lite11 liboce-ocaf11 liboce-visualization11 libpmem1
  libqt5positioning5 libqt5qml5 libqt5quick5 libqt5sensors5 libqt5webchannel5
  libqt5webkit5 librados2 librbd1 librdmacm1 libslirp0 libspice-server1
  libstring-shellquote-perl libsys-virt-perl libtsk13 libusbredirparser1
  libvirglrenderer1 libvirt0 libvncserver1 libwin-hivex-perl libyara3
  livescript lsscsi lvm2 mocha module-assistant msr-tools node-ansi-escapes
  node-anymatch node-argparse node-async-each node-binary-extensions
  node-braces node-browser-stdout node-chokidar node-cli-cursor node-cli-width
  node-commander node-debbundle-acorn node-deep-is node-diff node-doctrine
  node-eslint-scope node-eslint-visitor-keys node-espree node-esprima
  node-esquery node-esrecurse node-estraverse node-esutils node-exit-hook
  node-external-editor node-fast-levenshtein node-file-entry-cache
  node-fill-range node-flat-cache node-flatted node-functional-red-black-tree
  node-glob-parent node-globals node-growl node-he node-ignore node-inquirer
  node-is-binary-path node-is-buffer node-is-extglob node-is-glob
  node-is-number node-is-promise node-js-yaml node-jschardet node-kind-of
  node-levn node-normalize-path node-optionator node-path-dirname
  node-prelude-ls node-progress node-readdirp node-regenerate
  node-regenerate-unicode-properties node-repeat-string node-restore-cursor
  node-run-async node-rx node-set-immediate-shim node-source-map
  node-source-map-support node-sprintf-js node-tmp node-to-regex-range
  node-type-check node-wordwrap node-write osinfo-db ovmf qemu-block-extra
  qemu-system-common qemu-system-data qemu-system-x86 qemu-utils
  qml-module-qtgraphicaleffects qml-module-qtquick-controls
  qml-module-qtquick-dialogs qml-module-qtquick-layouts
  qml-module-qtquick-privatewidgets qml-module-qtquick-window2
  qml-module-qtquick2 ruby-bcrypt-pbkdf ruby-builder ruby-childprocess
  ruby-concurrent ruby-domain-name ruby-ed25519 ruby-erubis ruby-excon
  ruby-ffi ruby-fog-core ruby-fog-json ruby-fog-libvirt ruby-fog-xml
  ruby-formatador ruby-http-cookie ruby-i18n ruby-libvirt ruby-listen
  ruby-log4r ruby-mime-types ruby-mime-types-data ruby-multi-json ruby-net-scp
  ruby-net-sftp ruby-net-ssh ruby-netrc ruby-nokogiri ruby-oj ruby-pkg-config
  ruby-rb-inotify ruby-rest-client ruby-sqlite3 ruby-unf ruby-unf-ext
  ruby-vagrant-cloud ruby-zip scrub seabios sleuthkit sqlite3 supermin
  thin-provisioning-tools virtualbox-source zerofree
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 16 not upgraded.
## Provides modern Erlang/OTP releases
##
deb [signed-by=/usr/share/keyrings/io.cloudsmith.rabbitmq.E495BB49CC4BBE5B.gpg] https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/deb/ubuntu bionic main
deb-src [signed-by=/usr/share/keyrings/io.cloudsmith.rabbitmq.E495BB49CC4BBE5B.gpg] https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/deb/ubuntu bionic main

## Provides RabbitMQ
##
deb [signed-by=/usr/share/keyrings/io.cloudsmith.rabbitmq.9F4587F226208342.gpg] https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/deb/ubuntu bionic main
deb-src [signed-by=/usr/share/keyrings/io.cloudsmith.rabbitmq.9F4587F226208342.gpg] https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/deb/ubuntu bionic main
Hit:1 http://ke.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://ke.archive.ubuntu.com/ubuntu focal-updates InRelease              
Hit:3 http://ke.archive.ubuntu.com/ubuntu focal-backports InRelease            
Hit:4 http://dl.google.com/linux/chrome/deb stable InRelease                   
Hit:5 http://security.ubuntu.com/ubuntu focal-security InRelease               
Hit:7 https://packages.microsoft.com/repos/ms-teams stable InRelease           
Get:8 https://mega.nz/linux/MEGAsync/xUbuntu_20.04 ./ InRelease [2,441 B]      
Get:9 https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/deb/ubuntu bionic InRelease [5,179 B]
Get:10 https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/deb/ubuntu bionic InRelease [5,143 B]
Hit:6 https://packages.cloud.google.com/apt kubernetes-xenial InRelease        
Hit:11 http://ppa.launchpad.net/audio-recorder/ppa/ubuntu focal InRelease
Hit:12 http://ppa.launchpad.net/kicad/kicad-5.1-releases/ubuntu focal InRelease
Hit:13 http://ppa.launchpad.net/linuxuprising/java/ubuntu focal InRelease      
Hit:15 http://ppa.launchpad.net/ondrej/php/ubuntu focal InRelease              
Hit:16 http://ppa.launchpad.net/vbernat/haproxy-2.2/ubuntu focal InRelease     
Hit:14 https://packagecloud.io/AtomEditor/atom/any any InRelease               
Hit:17 https://linux.teamviewer.com/deb stable InRelease                       
Fetched 12.8 kB in 11s (1,180 B/s)                                             
Reading package lists... Done
Reading package lists... Done
Building dependency tree       
Reading state information... Done
erlang-asn1 is already the newest version (1:24.1.4-1).
erlang-asn1 set to manually installed.
erlang-base is already the newest version (1:24.1.4-1).
erlang-crypto is already the newest version (1:24.1.4-1).
erlang-crypto set to manually installed.
erlang-eldap is already the newest version (1:24.1.4-1).
erlang-eldap set to manually installed.
erlang-ftp is already the newest version (1:24.1.4-1).
erlang-ftp set to manually installed.
erlang-inets is already the newest version (1:24.1.4-1).
erlang-inets set to manually installed.
erlang-mnesia is already the newest version (1:24.1.4-1).
erlang-mnesia set to manually installed.
erlang-os-mon is already the newest version (1:24.1.4-1).
erlang-os-mon set to manually installed.
erlang-parsetools is already the newest version (1:24.1.4-1).
erlang-parsetools set to manually installed.
erlang-public-key is already the newest version (1:24.1.4-1).
erlang-public-key set to manually installed.
erlang-runtime-tools is already the newest version (1:24.1.4-1).
erlang-runtime-tools set to manually installed.
erlang-snmp is already the newest version (1:24.1.4-1).
erlang-snmp set to manually installed.
erlang-ssl is already the newest version (1:24.1.4-1).
erlang-ssl set to manually installed.
erlang-syntax-tools is already the newest version (1:24.1.4-1).
erlang-syntax-tools set to manually installed.
erlang-tftp is already the newest version (1:24.1.4-1).
erlang-tftp set to manually installed.
erlang-tools is already the newest version (1:24.1.4-1).
erlang-tools set to manually installed.
erlang-xmerl is already the newest version (1:24.1.4-1).
erlang-xmerl set to manually installed.
The following packages were automatically installed and are no longer required:
  augeas-lenses cpu-checker db-util db5.3-util dctrl-tools dkms dmeventd
  extlinux gconf-service gconf-service-backend gconf2-common hfsplus
  ibverbs-providers icoutils ipxe-qemu ipxe-qemu-256k-compat-efi-roms kbuild
  kpartx ldmtool libafflib0v5 libarchive-tools libaugeas0 libcacard0
  libconfig9 libdate-manip-perl libdevmapper-event1.02.1 libewf2 libfdt1
  libgconf-2-4 libgl2ps1.4 libgsoap-2.8.91 libguestfs-hfsplus libguestfs-perl
  libguestfs-reiserfs libguestfs-tools libguestfs-xfs libguestfs0 libhfsp0
  libhivex0 libibverbs1 libintl-perl libintl-xs-perl libiscsi7
  libjs-regenerate libjs-source-map libjs-sprintf-js libldm-1.0-0 libllvm11
  liblvm2cmd2.03 liblzf1 liboce-foundation11 liboce-modeling11
  liboce-ocaf-lite11 liboce-ocaf11 liboce-visualization11 libpmem1
  libqt5positioning5 libqt5qml5 libqt5quick5 libqt5sensors5 libqt5webchannel5
  libqt5webkit5 librados2 librbd1 librdmacm1 libslirp0 libspice-server1
  libstring-shellquote-perl libsys-virt-perl libtsk13 libusbredirparser1
  libvirglrenderer1 libvirt0 libvncserver1 libwin-hivex-perl libyara3
  livescript lsscsi lvm2 mocha module-assistant msr-tools node-ansi-escapes
  node-anymatch node-argparse node-async-each node-binary-extensions
  node-braces node-browser-stdout node-chokidar node-cli-cursor node-cli-width
  node-commander node-debbundle-acorn node-deep-is node-diff node-doctrine
  node-eslint-scope node-eslint-visitor-keys node-espree node-esprima
  node-esquery node-esrecurse node-estraverse node-esutils node-exit-hook
  node-external-editor node-fast-levenshtein node-file-entry-cache
  node-fill-range node-flat-cache node-flatted node-functional-red-black-tree
  node-glob-parent node-globals node-growl node-he node-ignore node-inquirer
  node-is-binary-path node-is-buffer node-is-extglob node-is-glob
  node-is-number node-is-promise node-js-yaml node-jschardet node-kind-of
  node-levn node-normalize-path node-optionator node-path-dirname
  node-prelude-ls node-progress node-readdirp node-regenerate
  node-regenerate-unicode-properties node-repeat-string node-restore-cursor
  node-run-async node-rx node-set-immediate-shim node-source-map
  node-source-map-support node-sprintf-js node-tmp node-to-regex-range
  node-type-check node-wordwrap node-write osinfo-db ovmf qemu-block-extra
  qemu-system-common qemu-system-data qemu-system-x86 qemu-utils
  qml-module-qtgraphicaleffects qml-module-qtquick-controls
  qml-module-qtquick-dialogs qml-module-qtquick-layouts
  qml-module-qtquick-privatewidgets qml-module-qtquick-window2
  qml-module-qtquick2 ruby-bcrypt-pbkdf ruby-builder ruby-childprocess
  ruby-concurrent ruby-domain-name ruby-ed25519 ruby-erubis ruby-excon
  ruby-ffi ruby-fog-core ruby-fog-json ruby-fog-libvirt ruby-fog-xml
  ruby-formatador ruby-http-cookie ruby-i18n ruby-libvirt ruby-listen
  ruby-log4r ruby-mime-types ruby-mime-types-data ruby-multi-json ruby-net-scp
  ruby-net-sftp ruby-net-ssh ruby-netrc ruby-nokogiri ruby-oj ruby-pkg-config
  ruby-rb-inotify ruby-rest-client ruby-sqlite3 ruby-unf ruby-unf-ext
  ruby-vagrant-cloud ruby-zip scrub seabios sleuthkit sqlite3 supermin
  thin-provisioning-tools virtualbox-source zerofree
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 16 not upgraded.
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  augeas-lenses cpu-checker db-util db5.3-util dctrl-tools dkms dmeventd
  extlinux gconf-service gconf-service-backend gconf2-common hfsplus
  ibverbs-providers icoutils ipxe-qemu ipxe-qemu-256k-compat-efi-roms kbuild
  kpartx ldmtool libafflib0v5 libarchive-tools libaugeas0 libcacard0
  libconfig9 libdate-manip-perl libdevmapper-event1.02.1 libewf2 libfdt1
  libgconf-2-4 libgl2ps1.4 libgsoap-2.8.91 libguestfs-hfsplus libguestfs-perl
  libguestfs-reiserfs libguestfs-tools libguestfs-xfs libguestfs0 libhfsp0
  libhivex0 libibverbs1 libintl-perl libintl-xs-perl libiscsi7
  libjs-regenerate libjs-source-map libjs-sprintf-js libldm-1.0-0 libllvm11
  liblvm2cmd2.03 liblzf1 liboce-foundation11 liboce-modeling11
  liboce-ocaf-lite11 liboce-ocaf11 liboce-visualization11 libpmem1
  libqt5positioning5 libqt5qml5 libqt5quick5 libqt5sensors5 libqt5webchannel5
  libqt5webkit5 librados2 librbd1 librdmacm1 libslirp0 libspice-server1
  libstring-shellquote-perl libsys-virt-perl libtsk13 libusbredirparser1
  libvirglrenderer1 libvirt0 libvncserver1 libwin-hivex-perl libyara3
  livescript lsscsi lvm2 mocha module-assistant msr-tools node-ansi-escapes
  node-anymatch node-argparse node-async-each node-binary-extensions
  node-braces node-browser-stdout node-chokidar node-cli-cursor node-cli-width
  node-commander node-debbundle-acorn node-deep-is node-diff node-doctrine
  node-eslint-scope node-eslint-visitor-keys node-espree node-esprima
  node-esquery node-esrecurse node-estraverse node-esutils node-exit-hook
  node-external-editor node-fast-levenshtein node-file-entry-cache
  node-fill-range node-flat-cache node-flatted node-functional-red-black-tree
  node-glob-parent node-globals node-growl node-he node-ignore node-inquirer
  node-is-binary-path node-is-buffer node-is-extglob node-is-glob
  node-is-number node-is-promise node-js-yaml node-jschardet node-kind-of
  node-levn node-normalize-path node-optionator node-path-dirname
  node-prelude-ls node-progress node-readdirp node-regenerate
  node-regenerate-unicode-properties node-repeat-string node-restore-cursor
  node-run-async node-rx node-set-immediate-shim node-source-map
  node-source-map-support node-sprintf-js node-tmp node-to-regex-range
  node-type-check node-wordwrap node-write osinfo-db ovmf qemu-block-extra
  qemu-system-common qemu-system-data qemu-system-x86 qemu-utils
  qml-module-qtgraphicaleffects qml-module-qtquick-controls
  qml-module-qtquick-dialogs qml-module-qtquick-layouts
  qml-module-qtquick-privatewidgets qml-module-qtquick-window2
  qml-module-qtquick2 ruby-bcrypt-pbkdf ruby-builder ruby-childprocess
  ruby-concurrent ruby-domain-name ruby-ed25519 ruby-erubis ruby-excon
  ruby-ffi ruby-fog-core ruby-fog-json ruby-fog-libvirt ruby-fog-xml
  ruby-formatador ruby-http-cookie ruby-i18n ruby-libvirt ruby-listen
  ruby-log4r ruby-mime-types ruby-mime-types-data ruby-multi-json ruby-net-scp
  ruby-net-sftp ruby-net-ssh ruby-netrc ruby-nokogiri ruby-oj ruby-pkg-config
  ruby-rb-inotify ruby-rest-client ruby-sqlite3 ruby-unf ruby-unf-ext
  ruby-vagrant-cloud ruby-zip scrub seabios sleuthkit sqlite3 supermin
  thin-provisioning-tools virtualbox-source zerofree
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
  rabbitmq-server
0 upgraded, 1 newly installed, 0 to remove and 16 not upgraded.
Need to get 0 B/12.5 MB of archives.
After this operation, 30.2 MB of additional disk space will be used.
Selecting previously unselected package rabbitmq-server.
(Reading database ... 396199 files and directories currently installed.)
Preparing to unpack .../rabbitmq-server_3.9.8-1_all.deb ...
Unpacking rabbitmq-server (3.9.8-1) ...
Setting up rabbitmq-server (3.9.8-1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/rabbitmq-server.service → /lib/systemd/system/rabbitmq-server.service.
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for systemd (245.4-4ubuntu3.13) ...

Installing Celery

we will install celery via pip by running the following command

pip install celery

Creating your first celery task

A Celery application acts as an entry point for all the work that you do in celery from tasks to management of workers.

We will use a single python module which will contain everything for this simple tutorial.

from celery import Celery

app = Celery('tasks', broker='pyamqp://guest@localhost//')

@app.task
def add(x, y):
    return x + y

The Celery class imported from Celery takes 2 arguments, the name of the current module and a keyword argument for the message broker in use. In this case we are using the rabbitMQ message broker.

To run the worker we are going to execute the program with the worker argument. We will run this in one terminal window and leave it open but in production the program should be executed in the background as a daemon.


elery -A tasks worker --loglevel=INFO

$celery -A tasks worker --loglevel=INFO
 
 -------------- celery@mutune v5.1.2 (sun-harmonics)
--- ***** ----- 
-- ******* ---- Linux-5.11.0-38-generic-x86_64-with-glibc2.29 2021-11-03 00:07:38
- *** --- * --- 
- ** ---------- [config]
- ** ---------- .> app:         hello:0x7f8b4d7eb5b0
- ** ---------- .> transport:   amqp://guest:**@localhost:5672//
- ** ---------- .> results:     disabled://
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery
                

[tasks]
  . tasks.hello

[2021-11-03 00:07:38,409: INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672//
[2021-11-03 00:07:38,422: INFO/MainProcess] mingle: searching for neighbors
[2021-11-03 00:07:39,458: INFO/MainProcess] mingle: all alone
[2021-11-03 00:07:39,482: INFO/MainProcess] celery@mutune ready.
[2021-11-03 00:09:07,052: INFO/MainProcess] Task tasks.hello[0bc3d978-0639-422a-b958-fca8ac14097a] received
[2021-11-03 00:09:07,053: INFO/ForkPoolWorker-2] Task tasks.hello[0bc3d978-0639-422a-b958-fca8ac14097a] succeeded in 0.00022812099996372126s: 'Hello world'

In another terminal window we will enter python REPL mode and call our task using the delay method.

Python 3.8.10 (default, Sep 28 2021, 16:10:42) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from tasks import hello
>>> hello.delay()
<AsyncResult: 95119e52-36e5-4ccd-9e06-3ea2da1a5bd7>
>>> 

Immediately after calling the task you can confirm that the task has been processed by the worker that you started earlier in the first terminal window ie in the worker’s console output.

When a task is called, an AsyncResult instance is returned, this instance can be used to wait for the task to finish, get its return value and to check the state of the task.

That’s it! You have your first celery implementation running.

Next Steps.

This tutorial gave you a basic introduction to Celery integration with Python. To learn more about celery Check the full celery documentation .