【发布时间】:2016-12-05 15:33:35
【问题描述】:
我正在尝试运行 2 个线程,第一个线程有一个 function1 目标,这个函数应该从机器读取一个值,当这个值 =0 时,输出 0 保存在一个数组中。当此值不再为 0 时,输出 1 应保存在此数组中。然后队列必须返回这个列表。第二个线程有一个function2作为目标,这个函数正在做其他事情。我将尝试在以下代码中显示它:
import threading
from multiprocessing import Queue
def func1(queue_in):
list=[]
while value_from_machine==0: #this value will always be refreshed and read again
list.append(0)
queue_in.put(list)
list.append(1) #when the value from the machine is not 0 anymore, put a 1 in the list
queue_in.put(list)
def func2():
#doing something else...
q_out=Queue()
thread1=threading.Thread(target=func1,args=(q_out))
thread2=threading.Thread(target=func2)
thread1.start()
thread2.start()
q_value=q_out.get()
if sum(q_value)==1:
#print something
else:
#print something else
现在的问题是我希望第一个线程在第二个线程完成后停止。另一件事是我不确定队列是第一个函数中的输出。在while循环中有队列好不好??
【问题讨论】:
标签: python multithreading queue python-multithreading