factory/apps/utils/thread.py

23 lines
777 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import threading
from apps.utils.decorators import auto_log
from concurrent.futures import ThreadPoolExecutor
from django.db import connections
# 创建全局线程池
global_executor = ThreadPoolExecutor(max_workers=20)
class MyThread(threading.Thread):
@auto_log('MyThread', raise_exception=True, send_mail=True)
def run(self) -> None:
# 子线程退出 / 池内 worker 跑完一次任务后必须关闭本线程的 Django DB 连接,
# 否则 psycopg2 连接会一直驻留在线程的 thread-local导致 PG "too many clients"
try:
return super().run()
finally:
connections.close_all()
def start_p(self):
"""
使用线程池启动
"""
global_executor.submit(self.run)