tomcounsell tomcounsell - 4 months ago 184
Python Question

Multithreading for Python Django

Some functions should run asynchronously on the web server. Sending emails is a classic example.

What is the best (or most pythonic) way write a decorator function to run a function asynchronously?

My setup is a common one: Python 2.4.7, Django 1.4, Gunicorn 0.17.2

For example, here's a start:

from threading import Thread

def postpone(function):
def decorator(*args, **kwargs):
t = Thread(target = function, args=args, kwargs=kwargs)
t.daemon = True
t.start()
return decorator


desired usage:

@postpone
def foo():
pass #do stuff

Answer

I posted the question above 18 months ago. Since then, I've continued using this implementation at scale and in production with no issues.

decorator definition:

from threading import Thread
def postpone(function):
  def decorator(*args, **kwargs):
    t = Thread(target = function, args=args, kwargs=kwargs)
    t.daemon = True
    t.start()
  return decorator

example usage:

@postpone
def foo():
  #do stuff

Over time, the stack has updated and transitioned without fail.

originally Python 2.4.7, Django 1.4, Gunicorn 0.17.2

now Python 2.4.7, Django 1.9, Waitress 0.8.9

Comments