python - 如何在python中的多个线程之间正确共享信息?
问题描述
我正在构建一个系统,该系统需要从一个线程(第一个生成信息)与第二个线程(套接字)共享信息。
如何访问socketThread中Detector Thread的信息?我尝试将变量设置为全局,它们可以在 main.py 文件中找到。虽然我无法在套接字线程中访问它们。
from objDetect import *
from socketclient import *
import threading
detectorThread = threading.Thread(name='detect', target=detectorWorker)
socketThread = threading.Thread(name='api', target=runSocket)
socketThread.setDaemon(True)
try:
detectorThread.start()
socketThread.start()
except:
print('[ERROR] Couldnt start detector.')
解决方案
您可以使用模块中的各种线程安全队列之一queue
。
推荐阅读
- r - 返回数据框中包含 R 中“x”元素的行
- blazor - Blazor - 检测任何表单值何时更改
- python - 在 Python 中计算浮点数数组的 crc32 校验和
- php - 在 foreach 中的两个 div 类之间交换
- java - 有没有办法以java 8格式编写下面的代码?
- c++ - 如何显示和填充到ostream?
- python - 使用python脚本将(多个)TeX环境提取到csv
- regex - re 模式不识别逗号值
- python - 怎么说如果没有找到日期值
- html - 可以创建一个 HTML 链接,从给定的 doi 返回一个 bibtex 条目,该链接可以在 GitHub wiki 中工作吗?