Python - 多处理错误“无法启动进程两次" [英] Python - Multiprocessing Error 'cannot start a process twice'

查看:49
本文介绍了Python - 多处理错误“无法启动进程两次"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 Python 中的 multiprocessing 包开发算法,我从互联网上学习了一些教程,并尝试使用此包开发算法.在环顾四周并使用 ProcessQueuePool 尝试我的hello world"之后,我尝试在此代码上实现队列

I try to develop an algorithm using multiprocessing package in Python, i learn some tutorial from internet and try to develop an algorithm with this package. After looking around and try my 'hello world' using Process, Queue and Pool, i try to implement the Queue on this code

def main(queue):
   d = ...
   k = ...
   filename, patname, txt, pat = ...
   R = queue
   processes = []

   for j in range(k-1):
        processes.append(Process(target=sim, args=(int(j * d), int((j+1) * d), txt, pat, filename, patname, R, )))

   # processes.append(Process(target=sim, args=(int(j * d), len(txt), txt, pat, filename, patname, R, )))       

   for pr in processes:
        pr.start()

   for pr in processes:
        pr.join()

   while not R.empty():
        print (R.get())

if __name__ == '__main__':
    R = Queue()
    main(R)

但是,得到如下错误:

AssertionError: Cannot start a process twice

有人可以帮忙解决这个问题吗

Can somebody please help with this issue

完整输出:

sim(e_original.txt, e_modify0%.txt) = 0.000000
sim(e_original.txt, e_modify0%.txt) = 0.000000
1
Traceback (most recent call last):
  File "measure.py", line 108, in <module>
    main()
  File "measure.py", line 98, in main
    pr.start()
  File "C:\Python27\lib\multiprocessing\process.py", line 120, in start
    assert self._popen is None, 'cannot start a process twice'
AssertionError: cannot start a process twice
sim(e_original.txt, e_modify0%.txt) = 0.000000

推荐答案

(SOLVED)这是我问题的答案,抱歉迟到了.

(SOLVED)This is the answer of my question, sorry for the late post.

for j in range(k-1):
    p = Process(target=prk.sim, args=(int(j * d), int((j+1) * d) + 5 - 1,))
    processes.append(p)
    p.start()

p = Process(target=prk.sim, args=(int(d * (k-1)), txtlen,))                     
processes.append(p)
p.start()   

如果 k = 3 我需要 3 个处理我的应用程序的进程.对于第一个循环,我运行 Process 两次,所以我每次迭代都启动 Process.所以,我删除了这段代码

in case that k = 3 i need 3 Process that working on my app. For the first loop i run the Process twice, so i just start the Process each iteration. So, i delete this code

for pr in processes:
    pr.start()

for pr in processes:
    pr.join()

谢谢大家的回复.

这篇关于Python - 多处理错误“无法启动进程两次"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆