您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

适用于Python Django的多线程

适用于Python Django的多线程

我继续在大规模生产中使用此实现,没有任何问题。

装饰器定义:

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()
python 2022/1/1 18:39:26 有255人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶