@@ -54,8 +54,8 @@ In this tutorial, we will go over how to run tasks asynchronously in
5454your TOM if you have the need to do so.
5555
5656Running tasks with django-tasks
57- ~~~~~~~~~~~~~~~~~~~~~~~~~~~
58- `django-tasks <https://github.com/django/deps/blob/a83080652411e34e6afa8e1f0a97b675a76358e5/accepted/0014-background-workers.rst> `
57+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
58+ `django-tasks <https://github.com/django/deps/blob/a83080652411e34e6afa8e1f0a97b675a76358e5/accepted/0014-background-workers.rst >`__
5959is a reference implementation of Django’s official background tasks library.
6060It provides an interface for marking functions as tasks and a worker
6161for executing them. The database backend utilizes the Django ORM,
@@ -66,7 +66,9 @@ Setting up django-tasks in a TOM
6666^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6767Check to make sure your TOM has django_tasks installed:
6868
69- .. code :: python
69+ .. code-block :: python
70+ :caption: settings.py
71+
7072 INSTALLED_APPS = [
7173 ...
7274 ' django_tasks' ,
@@ -78,7 +80,9 @@ By default, the immediate mode is enabled which means tasks are not run
7880asynchronously. To enable asynchronous execution, you need to configure
7981the django_tasks to use a the database backend:
8082
81- .. code :: python
83+ .. code-block :: python
84+ :caption: settings.py
85+
8286 TASKS = {
8387 " default" : {
8488 " BACKEND" : " django_tasks.backends.database.DatabaseBackend"
@@ -119,22 +123,24 @@ you can do so with:
119123
120124In ``tasks.py ``:
121125
122- .. code :: python
126+ .. code-block :: python
127+ :caption: mytom/ myapp/ tasks.py
128+ :linenos:
123129
124- from django_tasks import task
125- import time
126- import logging
130+ from django_tasks import task
131+ import time
132+ import logging
127133
128- logger = logging.getLogger(__name__ )
134+ logger = logging.getLogger(__name__ )
129135
130136
131- @task
132- def super_complicated_task ():
133- logger.info(' starting task...' )
134- time.sleep(2 )
135- logger.info(' still running...' )
136- time.sleep(2 )
137- logger.info(' done!' )
137+ @task
138+ def super_complicated_task ():
139+ logger.info(' starting task...' )
140+ time.sleep(2 )
141+ logger.info(' still running...' )
142+ time.sleep(2 )
143+ logger.info(' done!' )
138144
139145 This task will emulate a function that blocks for 4 seconds, in practice
140146this would be a network call or some kind of heavy processing task.
@@ -164,15 +170,15 @@ shell!) you should see the following output:
164170 done!
165171 Task id=f323fdc8-4088-424d-a4d4-74ad741c5c04 path=tom_async_demo.views.super_complicated_task state=SUCCEEDED
166172
167- Notice how calling the enqueue() function returned immediately in the shell, but the
173+ Notice how calling the `` enqueue() `` function returned immediately in the shell, but the
168174task took a few seconds to complete. This is how it would work in
169175practice in your django app: Somewhere in your code, for example in your
170176app’s ``views.py ``, you would import the task just like we did in the
171177terminal. Now when the view gets called, the task will be queued for
172178execution and the response can be sent back to the user’s browser right
173179away. The task will finish in the background.
174180
175- A few more things about the enqueue() function: First, if your task function
181+ A few more things about the `` enqueue() `` function: First, if your task function
176182takes any arguments, you pass them into the enqueue function. Secondly,
177183the object returned from this function is a TaskResult. This object can be used to
178184check the status of the task, retrieve its result, or cancel it.
0 commit comments