mirror of
https://github.com/HackSoftware/Django-Styleguide.git
synced 2025-02-18 04:20:47 +03:00
Add subsection for circular import issues between tasks & services
This commit is contained in:
parent
c07124b779
commit
1a77336e4f
64
README.md
64
README.md
|
@ -45,6 +45,7 @@ Expect often updates as we discuss & decide upon different things.
|
||||||
* [Structure](#structure)
|
* [Structure](#structure)
|
||||||
+ [Configuration](#configuration)
|
+ [Configuration](#configuration)
|
||||||
+ [Tasks](#tasks)
|
+ [Tasks](#tasks)
|
||||||
|
+ [Circular imports between tasks & services](#circular-imports-between-tasks--services)
|
||||||
* [Periodic Tasks](#periodic-tasks)
|
* [Periodic Tasks](#periodic-tasks)
|
||||||
* [Configuration](#configuration-1)
|
* [Configuration](#configuration-1)
|
||||||
- [Inspiration](#inspiration)
|
- [Inspiration](#inspiration)
|
||||||
|
@ -1175,6 +1176,69 @@ Meaning, you can end up with `tasks/domain_a.py` and `tasks/domain_b.py`. All yo
|
||||||
|
|
||||||
The general rule of thumb is - split your tasks in a way that'll make sense to you.
|
The general rule of thumb is - split your tasks in a way that'll make sense to you.
|
||||||
|
|
||||||
|
#### Circular imports between tasks & services
|
||||||
|
|
||||||
|
In some cases, you need invoke a task from a service or vice-versa:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# project/app/services.py
|
||||||
|
|
||||||
|
from project.app.tasks import task_function_1
|
||||||
|
|
||||||
|
|
||||||
|
def service_function_1():
|
||||||
|
print('I delay a task!')
|
||||||
|
task_function_1.delay()
|
||||||
|
|
||||||
|
|
||||||
|
def service_function_2():
|
||||||
|
print('I do not delay a task!')
|
||||||
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
# project/app/tasks.py
|
||||||
|
|
||||||
|
from celery import shared_task
|
||||||
|
|
||||||
|
from project.app.services import service_function_2
|
||||||
|
|
||||||
|
|
||||||
|
@shared_task
|
||||||
|
def task_function_1():
|
||||||
|
print('I do not call a service!')
|
||||||
|
|
||||||
|
|
||||||
|
@shared_task
|
||||||
|
def task_function_2():
|
||||||
|
print('I call a service!')
|
||||||
|
service_function_2()
|
||||||
|
```
|
||||||
|
|
||||||
|
Unfortunately, this will result in a circular import.
|
||||||
|
|
||||||
|
What we usually do is we import the service function **inside** the task function:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# project/app/tasks.py
|
||||||
|
|
||||||
|
from celery import shared_task
|
||||||
|
|
||||||
|
|
||||||
|
@shared_task
|
||||||
|
def task_function_1():
|
||||||
|
print('I do not call a service!')
|
||||||
|
|
||||||
|
|
||||||
|
@shared_task
|
||||||
|
def task_function_2():
|
||||||
|
from project.app.services import service_function_2 # <--
|
||||||
|
|
||||||
|
print('I call a service!')
|
||||||
|
service_function_2()
|
||||||
|
```
|
||||||
|
|
||||||
|
* Note: Depending on the case, you may want to import the task function **inside** the service function. This is OK and will still prevent the circular import between service & task functions.
|
||||||
|
|
||||||
### Periodic Tasks
|
### Periodic Tasks
|
||||||
|
|
||||||
Managing periodic tasks is quite important, especially when you have tens, or hundreds of them.
|
Managing periodic tasks is quite important, especially when you have tens, or hundreds of them.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user