You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python threading同时运行两个脚本时仅第一个启动如何解决

问题成因

你创建threading.Thread对象时,给target参数传入的是videoStream()、expressions(),加括号的写法会直接在主线程中同步执行对应函数,而不是将函数作为目标交给子线程调度运行。
第一个函数videoStream()调用后会执行os.system('python cv2stream.py'),而你的cv2stream.py是永久循环的结构,os.system会一直阻塞等待子进程退出,主线程会卡在这一步,根本执行不到第二个线程的创建逻辑,所以第二个线程始终无法启动。
另外你给出的代码还漏了import threading语句,实际运行会直接抛导入错误。

修复方案

将target参数赋值为函数对象本身(去掉函数名后的括号),同时补全缺失的导入语句即可。修复后的代码如下:

import os
import threading

def videoStream():
    os.system('python cv2stream.py')

def expressions():
    os.system('python expression.py')
        
vs = threading.Thread(target=videoStream)
ex = threading.Thread(target=expressions)
vs.start()
ex.start()
vs.join()
ex.join()

内容的提问来源于stack exchange,提问作者Andrea Eusebio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 16:06:03