我继续在大规模生产中使用此实现,没有任何问题。
装饰器定义:
def start_new_thread(function):
def decorator(*args, **kwargs):
t = Thread(target = function, args=args, kwargs=kwargs)
t.daemon = True
t.start()
return decorator
用法示例:
@start_new_thread
def foo():
#do stuff
随着时间的流逝,堆栈已更新并成功过渡。
最初是Python 2.4.7,Django 1.4,Gunicorn 0.17.2,现在是Python 3.6,Django 2.1,Waitress 1.1。
如果您正在使用任何数据库事务,则Django将创建一个新连接,需要手动关闭该连接:
from django.db import connection
@postpone
def foo():
#do stuff
connection.close()