Package: negotium
NegotiumA 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.InstallationpipinstallnegotiumFeaturesAsynchronous task executionScheduled task executionDynamic periodic task executionTask cancellation: All tasks are cancellable using the UUID returned by the task execution methods:delay,apply_async, andapply_periodic_asyncUsage# ---- main.py (app entry point) ----fromnegotiumimportNegotiumfromnegotium.brokersimportRedis# create brokerbroker=Redis(host='localhost',port=6379,user='default',# optionalpassword='password',# optionaldb=0# optional (defaults to 0))# create negotium appapp=Negotium(app_name="<YOUR_APP_NAME>",broker=broker,log_file="<PATH_TO_LOG_FILE>"# optional. Defaults to stdout)app.start()@app.taskdefadd(x,y):returnx+yDelayed task executionadd.delay(1,2)Scheduled task executionadd.apply_async(args=(1,2),eta=datetime.datetime.now()+datetime.timedelta(seconds=10))Dynamic periodic task executionNote: Periodic tasks are scheduled usingnegotium.schedules.Crontabobject. TheCrontabobject 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.frommainimportappfromnegotium.schedulesimportCrontab# run the task every minutetask_id=add.apply_periodic_async(args=(1,2),cron=Crontab(minute=1))# with a raw crontab expressiontask_id=add.apply_periodic_async(args=(1,2),cron=Crontab(expression="* * * * *"))# to cancel, call the `cancel` methodapp.cancel(task_id)Using in a Django projectCreate anegotium.pyfile in your Django project directorymy_project/
    manage.py
    my_project/
        __init__.py
        settings.py
        urls.py
        negotium.py # <-- Create this file
        wsgi.pyAdd the following code to thenegotium.pyfileimportosfromnegotiumimportNegotiumfromnegotium.brokersimportRedis# Set the django settings moduleos.environ.setdefault('DJANGO_SETTINGS_MODULE','my_project.settings')# Create the brokerbroker=Redis(host='localhost',port=6379,user='default',# optionalpassword='password',# optionaldb=0# optional (defaults to 0))# Create the negotium appapp=Negotium(app_name="<YOUR_APP_NAME>",broker=broker)app.start()Import theappobject in your task modules# --- example/tasks.py ---frommy_project.negotiumimportapp@app.taskdefadd(x,y):returnx+yIn your views, you can run the task asynchronously# --- example/views.py ---fromexample.tasksimportaddfromnegotium.schedulesimportCrontabdefmy_async_view(request):add.delay(1,2)# <-- Run the task asynchronouslyreturnHttpResponse("Hello, world!")defmy_scheduled_view(request):add.apply_async(# <-- Schedule the task to run at a specific timeargs=(1,2),eta=datetime.datetime.now()+datetime.timedelta(seconds=10))returnHttpResponse("Hello, world!")defmy_periodic_view(request):add.apply_periodic_async(args=(1,2),cron=Crontab(expression="* * * * *"))returnHttpResponse("Hello, world!")You're all set! Now you can run the Django development server and start using negotium.