我在python中使用了多处理,并且成功地使用了队列,但是在进程仍在执行时,我需要(从main)监视一些变量。你知道吗
我知道使用全局变量不是一个好的做法,但即使这种方法也不起作用。你知道吗
有人能给我指出正确的方向吗?你知道吗
提前谢谢
GCCruz公司
附录: 我贴了一个我想做的简单例子:
import multiprocessing
import time
def sampleprocess(array, count):
'''process with heavy image processing in a loop'''
for i in range(count)
# Do processing on this array that outputs a given variable
sample_variable= count*10 # I would like to monitor this variable
if __name__ == '__main__':
p = multiprocessing.Process(target=sampleprocess, args=(array,1000,))
p.start()
# continuously monitor the dummy variable that is being computed on the process
while sample_variable < 1000
time.sleep(0.1)
print ' Still less than 1000'
一种选择是对共享数据对象使用多处理值和数组。 https://docs.python.org/2/library/multiprocessing.html#multiprocessing-managers
示例代码中的工作示例。如果你不止一个 进程同步写入将需要一个锁。你知道吗
从多进程导入进程、值、数组、锁 导入时间
def sampleprocess(s,count,lock): ''循环处理重图像'' 因为我在射程内(计数值): #对输出给定变量的数组执行处理
sample_variable=count*10#我想监视这个变量
如果名称=='main':
以下几个程序可能会演示如何实现解决方案:
例1
例2
例3
例4
正如你所看到的,有各种各样的方法来完成你所要求的任务。你知道吗
相关问题 更多 >
编程相关推荐