The uWSGI API is very low-level, as it must be language-independent.

That said, being too low-level is not a Good Thing for many languages, such as Python.

Decorators are, in our humble opinion, one of the more kick-ass features of Python, so in the uWSGI source tree you will find a module exporting a bunch of decorators that cover a good part of the uWSGI API.


Signal-based decorators execute the signal handler in the first available worker. If you have enabled the spooler you can execute the signal handlers in it, leaving workers free to manage normal requests. Simply pass target='spooler' to the decorator.

@timer(3, target='spooler')
def hello(signum):

Example: a Django session cleaner and video encoder

Let’s define a module and put it in the Django project directory.

import os
from django.contrib.sessions.models import Session
from django_uwsgi.decorators import cron, spool

@cron(40, 2, -1, -1, -1)
def clear_django_session(num):
    print("it's 2:40 in the morning: clearing django sessions")

def encode_video(arguments):
    os.system("ffmpeg -i \"%s\" image%%d.jpg" % arguments['filename'])

The session cleaner will be executed every day at 2:40, to enqueue a video encoding we simply need to spool it from somewhere else.

from tasks import encode_video

def index(request):
    # launching video encoding
    return render_to_response('enqueued.html')

Now run uWSGI with the spooler enabled:

; a couple of placeholder
django_projects_dir = /var/www/apps
my_project = foobar
; chdir to app project dir and set pythonpath
chdir = %(django_projects_dir)/%(my_project)
pythonpath = %(django_projects_dir)
; load django
module = django.core.handlers:WSGIHandler()
env = DJANGO_SETTINGS_MODULE=%(my_project).settings
; enable master
master = true
; 4 processes should be enough
processes = 4
; enable the spooler (the mytasks dir must exist!)
spooler = %(chdir)/mytasks
; load the module
import = task
; bind on a tcp socket
socket =

The only especially relevant option is the import one. It works in the same way as module but skips the WSGI callable search. You can use it to preload modules before the loading of WSGI apps. You can specify an unlimited number of ‘’‘import’‘’ directives.

django_uwsgi.decorators API reference


uWSGI is a preforking (or “fork-abusing”) server, so you might need to execute a fixup task after each fork(). The postfork decorator is just the ticket. You can declare multiple postfork tasks. Each decorated function will be executed in sequence after each fork().

def reconnect_to_db():

def hello_world():
    print("Hello World")

The uWSGI spooler can be very useful. Compared to Celery or other queues it is very “raw”. The spool decorator will help!

def a_long_long_task(arguments):
    for i in xrange(0, 10000000):

def a_longer_task(args):
    for i in xrange(0, 10000000):

# enqueue the tasks

The functions will automatically return uwsgi.SPOOL_OK so they will be executed one time independently by their return status.


Use spoolforever when you want to continuously execute a spool task. A @spoolforever task will always return uwsgi.SPOOL_RETRY.

def a_longer_task(args):
    for i in xrange(0, 10000000):

# enqueue the task

Advanced users may want to control the return value of a task.

def a_controlled_task(args):
    if args['foo'] == 'bar':
        return uwsgi.SPOOL_OK
    return uwsgi.SPOOL_RETRY

django_uwsgi.decorators.rpc("name", func)

uWSGI RPC is the fastest way to remotely call functions in applications hosted in uWSGI instances. You can easily define exported functions with the @rpc decorator.

def ciao_mondo_function():
    return "Hello World"

You can register signals for the signal framework in one shot.

def my_signal(num):
    print("i am signal %d" % num)
django_uwsgi.decorators.timer(interval, func)

Execute a function at regular intervals.

def three_seconds(num):
    print("3 seconds elapsed")
django_uwsgi.decorators.rbtimer(interval, func)

Works like @timer but using red black timers.

django_uwsgi.decorators.cron(min, hour, day, mon, wday, func)

Easily register functions for the CronInterface.

@cron(59, 3, -1, -1, -1)
def execute_me_at_three_and_fiftynine(num):
    print("it's 3:59 in the morning")

Since 1.2, a new syntax is supported to simulate crontab-like intervals (every Nth minute, etc.). */5 * * * * can be specified in uWSGI like thus:

@cron(-5, -1, -1, -1, -1)
def execute_me_every_five_min(num):
    print("5 minutes, what a long time!")
django_uwsgi.decorators.filemon(path, func)

Execute a function every time a file/directory is modified.

def tmp_has_been_modified(num):
    print("/tmp directory has been modified. Great magic is afoot")
django_uwsgi.decorators.erlang(process_name, func)

Map a function as an Erlang <> process.

def hello():
    return "Hello"

Mark function to be executed in a separate thread.


Threading must be enabled in uWSGI with the enable-threads or threads <n> option.

def a_running_thread():
    while True:
        print("i am a no-args thread")

def a_running_thread_with_args(who):
    while True:
        print("Hello %s (from arged-thread)" % who)


You may also combine @thread with @postfork to spawn the postfork handler in a new thread in the freshly spawned worker.

def a_post_fork_thread():
    while True:
        print("Hello from a thread in worker %d" % uwsgi.worker_id())

This decorator will execute a function in fully locked environment, making it impossible for other workers or threads (or the master, if you’re foolish or brave enough) to run it simultaneously. Obviously this may be combined with @postfork.

def dangerous_op():
    print("Concurrency is for fools!")
django_uwsgi.decorators.mulefunc([mulespec, ]func)

Offload the execution of the function to a mule <>. When the offloaded function is called, it will return immediately and execution is delegated to a mule.

def i_am_an_offloaded_function(argument1, argument2):
    print argument1,argument2

You may also specify a mule ID or mule farm to run the function on. Please remember to register your function with a uwsgi import configuration option.

def on_three():
    print "I'm running on mule 3."

def on_mcd():
    print "I'm running on a mule on Old McDonalds' farm."
django_uwsgi.decorators.harakiri(time, func)

Starting from uWSGI 1.3-dev, a customizable secondary harakiri subsystem has been added. You can use this decorator to kill a worker if the given call is taking too long.

def slow_function(foo, bar):
    for i in range(0, 10000):
        for y in range(0, 10000):

# or the alternative lower level api

uwsgi.set_user_harakiri(30) # you have 30 seconds. fight!
uwsgi.set_user_harakiri(0) # clear the timer, all is well