Skip to main content

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, and apply_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. The Crontab object takes the following arguments:

  • minute: The minute to run the task at
  • hour: The hour to run the task at
  • day: The day of the week to run the task at
  • month: The day of the month to run the task at
  • weekday: The month of the year to run the task at

Or, 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.

Release files for negotium 0.2.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for negotium 0.2.6
File Size Uploaded
negotium-0.2.6.tar.gz 14.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for negotium 0.2.6
File Interpreter ABI Platform
negotium-0.2.6-py3-none-any.whl Python 3 none any Details

Total release size: 30.8 kB

Release files / negotium-0.2.6.tar.gz

Download URL negotium-0.2.6.tar.gz
Size 14.6 kB
Tags Source
SHA-256 checksum
How to use checksums
8bb732e0e91d7e76f87d14b2c7f755aba8aa537f908262d83ec5291aed23a09c
BLAKE2b-256 checksum
How to use checksums
7b0980db4a96d92a56cd14e2bdbb0653185ff707b4a8a1e8a546e464c27c4c89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / negotium-0.2.6-py3-none-any.whl

Download URL negotium-0.2.6-py3-none-any.whl
Size 16.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
085be5246720b710d1f69f093edd07f6e1d0d8664b81aef4161ad60fb0b0039a
BLAKE2b-256 checksum
How to use checksums
b4544945971c80b78c43237bfe3e33542bb45599affd737784490d95e36f57f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page