Python服务器生成的新线程太多

2024-10-02 06:39:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个运行在python3.6.6上的Python Flask服务器,它出于某种原因生成新线程。这是我的密码:

# Packages and Modules
import os
import threading, time
from config import config
from Modeller.Modeller import Modeller

# Job scheduler function
def run_job(Modeller, config):
    while True:
        print("Run recurring task")
        print(threading.enumerate())
        modeller = Modeller(config) # Initiate modeller with config
        modeller.run()
        time.sleep(modeller.config['settings']['delay'])

# Start thread for calling function repeatedly
thread = threading.Thread(name='modelling', target=run_job, daemon=True, args=[Modeller, config])
thread.start()

第一个日志生成:

[<_MainThread(MainThread, started 140381841954560)>, <Thread(modelling, started daemon 140381169248000)>]

但经过几次运行后,它会产生:

[<_MainThread(MainThread, started 140381841954560)>, <Thread(modelling, started daemon 140381169248000)>, <Thread(Thread-1, started daemon 140381017106176)>, <Thread(Thread-2, started daemon 140380804740864)>, <Thread(Thread-3, started daemon 140380796348160)>, <Thread(Thread-4, started daemon 140380786644736)>, <Thread(Thread-5, started daemon 140380778252032)>]

唯一应该列出的线程是MainThread和modeling?我做错什么了?你知道吗


Tags: runfromimportconfigtime线程threaddaemon

热门问题