A lightweight and easy to use task queue/scheduler
Project description
Negotium
A simple, lightweight, and easy-to-use task/job queue for Python. It provides a simple API for running tasks asynchronously, scheduling tasks to run at a specific time, and running tasks periodically.
Unlike celery, negotium is designed to be simple and easy to use. It does not require a separate worker process to run tasks. Instead, it uses a single-threaded event loop to run tasks asynchronously. This makes it ideal for small to medium-sized projects that do not require the complexity of a full-blown task queue.
Negotium supports only Redis as a broker for now. Support for other brokers will be added in the future.
Installation
pip install negotium
Features
- Asynchronous task execution
- Scheduled task execution
- Dynamic periodic task execution
- Task cancellation: All tasks are cancellable using the UUID returned by the task execution methods:
delay
,apply_async
, andapply_periodic_async
Usage
# ---- main.py (app entry point) ----
from negotium import Negotium
from negotium.brokers import Redis
# create broker
broker = Redis(
host='localhost',
port=6379,
user='default', # optional
password='password', # optional
db=0 # optional (defaults to 0)
)
# create negotium app
app = Negotium(
app_name="<YOUR_APP_NAME>",
broker=broker,
log_file="<PATH_TO_LOG_FILE>" # optional. Defaults to stdout
)
app.start()
@app.task
def add(x, y):
return x + y
Delayed task execution
add.delay(1, 2)
Scheduled task execution
add.apply_async(args=(1, 2), eta=datetime.datetime.now() + datetime.timedelta(seconds=10))
Dynamic periodic task execution
Note: Periodic tasks are scheduled using
negotium.schedules.Crontab
object. TheCrontab
object takes the following arguments:
minute
: The minute to run the task athour
: The hour to run the task atday
: The day of the week to run the task atmonth
: The day of the month to run the task atweekday
: The month of the year to run the task atOr, you can pass a raw crontab expression as a string. For example:
* * * * *
will run the task every minute.
from main import app
from negotium.schedules import Crontab
# run the task every minute
task_id = add.apply_periodic_async(args=(1, 2), cron=Crontab(minute=1))
# with a raw crontab expression
task_id = add.apply_periodic_async(args=(1, 2), cron=Crontab(expression="* * * * *"))
# to cancel, call the `cancel` method
app.cancel(task_id)
Using in a Django project
- Create a
negotium.py
file in your Django project directory
my_project/
manage.py
my_project/
__init__.py
settings.py
urls.py
negotium.py # <-- Create this file
wsgi.py
- Add the following code to the
negotium.py
file
import os
from negotium import Negotium
from negotium.brokers import Redis
# Set the django settings module
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_project.settings')
# Create the broker
broker = Redis(
host='localhost',
port=6379,
user='default', # optional
password='password', # optional
db=0 # optional (defaults to 0)
)
# Create the negotium app
app = Negotium(app_name="<YOUR_APP_NAME>", broker=broker)
app.start()
- Import the
app
object in your task modules
# --- example/tasks.py ---
from my_project.negotium import app
@app.task
def add(x, y):
return x + y
- In your views, you can run the task asynchronously
# --- example/views.py ---
from example.tasks import add
from negotium.schedules import Crontab
def my_async_view(request):
add.delay(1, 2) # <-- Run the task asynchronously
return HttpResponse("Hello, world!")
def my_scheduled_view(request):
add.apply_async( # <-- Schedule the task to run at a specific time
args=(1, 2), eta=datetime.datetime.now() + datetime.timedelta(seconds=10)
)
return HttpResponse("Hello, world!")
def my_periodic_view(request):
add.apply_periodic_async(args=(1, 2), cron=Crontab(expression="* * * * *"))
return HttpResponse("Hello, world!")
You're all set! Now you can run the Django development server and start using negotium.
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
File details
Details for the file negotium-0.2.6.tar.gz
.
File metadata
- Download URL: negotium-0.2.6.tar.gz
- Upload date:
- Size: 14.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bb732e0e91d7e76f87d14b2c7f755aba8aa537f908262d83ec5291aed23a09c |
|
MD5 | 800506af357af8a79c23264ba988a305 |
|
BLAKE2b-256 | 7b0980db4a96d92a56cd14e2bdbb0653185ff707b4a8a1e8a546e464c27c4c89 |
File details
Details for the file negotium-0.2.6-py3-none-any.whl
.
File metadata
- Download URL: negotium-0.2.6-py3-none-any.whl
- Upload date:
- Size: 16.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 085be5246720b710d1f69f093edd07f6e1d0d8664b81aef4161ad60fb0b0039a |
|
MD5 | e810d21a8da4e7f2723a613b9725ceba |
|
BLAKE2b-256 | b4544945971c80b78c43237bfe3e33542bb45599affd737784490d95e36f57f1 |