A Django app for background task
Project description
Project
A Django background task app. By Bgtask, we can conveniently execute asynchronous tasks, and obtain the return data. The actuator runs in a separate process communicating with Django via TCP, ensuring it does not impact the main logic.
Usage
Install bgtask
pip install -U bgtask4django
Configurations
Add bgtask to INSTALLED_APPS in settings.py.
# settings.py
INSTALLED_APPS = [
'bgtask',
'...'
]
# Configuration should be read first from the Django settings,
# and if not found, it should be looked up from the system environment variables.
BGTASK_SERVER_IP = "127.0.0.1"
BGTASK_SERVER_PORT = 23323
BGTASK_WORK_THREADS = 10
Migrate
python manage.py migrate bgtask
Run server
bgtask run in a separate process, which is the same runtime environment as the Django instance.
python manage.py bgtaskserver
How to use in Django?
one demo
# views.py
import time
import pickle
from bgtask.client import submit
from bgtask.models import BgTask, StateEnum, ResultEnum
def send_email(address, title, message=None):
# some codes...
time.sleep(5)
# other codes...
def register(request, *args, **kwargs):
# `submit` will put function's import-path into task-queue by tcp,
# if no exception, it will return `task_id`.
task_id = submit(send_email, "*@*.com", "test", message="this is a test")
if not task_id:
print("send email fail, check log for reason")
...
# In other function or logic
# if we know task_id, we can get 'Bgtask' object, and then get task state and return data
task = BgTask.objects.get(pk=task_id)
print(task.state)
if task.state == StateEnum.FINISH and task.result == ResultEnum.SUCCESS:
print(pickle.loads(task.return_value))
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bgtask4django-1.0.2.tar.gz.
File metadata
- Download URL: bgtask4django-1.0.2.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bcb83c37129c64abe70ba7f73e5cf563b93740e52c708f3c40de3a4f7a71d006
|
|
| MD5 |
982cd03a4dd6fe0f5f3c2aea14740aed
|
|
| BLAKE2b-256 |
9b8fa2da9fcad8f02602fce7af22ea527ab48dc8b6c90a415c4e2cdcf0d0467a
|
File details
Details for the file bgtask4django-1.0.2-py3-none-any.whl.
File metadata
- Download URL: bgtask4django-1.0.2-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
65ed4a0ce75cb56cf646e9cd5b6549a769e9ade11f23afccc62fbf718b650335
|
|
| MD5 |
a84d9863211c6d7f52ae63b8573ee26a
|
|
| BLAKE2b-256 |
437394797aa921d5e172e66ed6274525a8907d5522e69521c6ee6c7312f047a5
|