我的目标是从另一个进程不断更新的python列表中读取值,我在这里尝试编写一个最小的工作代码。
我在cfg.py
中定义了一个全局数组作为
def initialize():
global infrared_frames
global depth_frames
infrared_frames = []
depth_frames = []
从main.py
开始,一个线程在该数组中追加一个随机值,而主线程休眠10秒,然后从该数组中读取该值。这是代码-
from multiprocessing import Process
import random
import time
import cfg
cfg.initialize()
def update_array():
while True:
cfg.infrared_frames.append(random.random())
p1 = Process(target=update_array)
p1.start()
time.sleep(10)
print('starting to read array')
print(cfg.infrared_frames)
for a in cfg.infrared_frames:
print(a)
不幸的是,当我尝试在循环之前和time.sleep()
调用之后打印数组时,数组仍然为空,但p1
进程正在运行。为什么我不能读取数组?
抱歉,如果这是一个简单的问题,我的知识是有限的,当谈到线程在python中
提前谢谢。
T
在多处理中添加和使用数据的一个好方法是使用
Queue
一个简单的工作示例:
说明
fill_queue
函数保持每秒向队列添加数据。然后由read_queue
函数处理。它将每隔0.2秒检查队列中是否有新项目相关问题 更多 >
编程相关推荐