我在线程上运行了很长时间。
import subprocess
import threading
def start_process():
target = subprocess.run(["python3", "workers.py"])
create_thread(target)
def create_thread(target):
threads = []
name = target.__name__
for t in threading.enumerate():
threads.append(t.name)
if name in threads:
return False # Thread exists
t = threading.Thread(None, target, name)
t.start()
return "Thread Created."
我创建了一个函数/烧瓶路由来监视线程以查看其是否处于活动状态。 此功能用于UI更新。
@app.route("/info", methods=['GET'])
def worker_info():
threads = []
for t in threading.enumerate():
threads.append(t.name)
return jsonify({
"thread_count" : threading.active_count(),
"ident" : threading.get_ident(),
"threads" : threads
})
问题在于,当我通过烧瓶路径调用threading.enumerate
时,线程名称并不总是出现。有时候会,有时候不会。