【发布时间】:2014-03-26 20:09:12
【问题描述】:
首先,我是多处理和线程领域的新手。我有两个生成数据的设备(gps 和 mca)。 gps 模拟器应该每 0.1 秒生成一个位置。 mca 应该在每个随机生成的时间间隔生成一个随机数。当mca注册一个事件时,计数(cnt)应该被发送到计数列表。gps也是如此。事件处理程序应该将计数与注册的最新 gps 值同步,这应该打印到标准输出。 5 秒后,mca 应该停止并通过队列发送“完成”以停止所有其他功能。我对队列也很陌生。在我看来,我的定义开始了,但什么也没做。
如果有人可以修复我的代码或让我知道其中出了什么问题,我将不胜感激。
import random
from multiprocessing import Process, Queue
from time import sleep, time, clock
count = []
gps_data = []
def mca(q1):
print 'started'
t = 5
while True:
cnt = random.randint(0,30)
count.append(cnt)
dt = random.randint(0,3)
sleep(dt)
nt = t-dt
if nt <= 0:
break
q1.put('DONE')
def gps(q1):
print 'started2'
while q1.get() != 'DONE':
x = 0
dt = 0.1
sleep(dt)
y = x + 1
gps_data.append(y)
def event_handler(q1):
print 'started3'
size_i = len(count) #initial size of the mca list
while q1.get() != 'DONE':
size_f = len(count)
if size_f > size_i:
local_count = count[-1]
local_location = gps_data[-1]
data = local_count + local_location
print str(data)
size_i = size_f
else:
pass
if __name__ == '__main__':
q1 = Queue()
p_mca = Process(target = mca, args = (q1,))
p_gps = Process(target = gps, args = (q1,))
p_evh = Process(target = event_handler, args = (q1,))
p_evh.start()
p_gps.start()
p_mca.start()
p_evh.join()
p_gps.join()
p_mca.join()
【问题讨论】:
标签: python multithreading queue multiprocessing