23 lines
777 B
Python
23 lines
777 B
Python
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) |