Python - 多处理错误'无法启动进程两次'

时间:2014-12-09 18:19:05

标签: python multiprocessing

我尝试使用Python中的multiprocessing包开发一个算法,我从互联网上学习一些教程并尝试用这个包开发算法。在浏览并使用ProcessQueuePool尝试“hello world”后,我尝试在此代码上实现队列

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

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

完整输出:

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

3 个答案:

答案 0 :(得分:1)

已解决)这是我的问题的答案,对于已故的帖子感到抱歉。

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。所以,我删除了这段代码

for pr in processes:
    pr.start()

for pr in processes:
    pr.join()

感谢您的回复。

答案 1 :(得分:0)

您收到断言是因为您多次在单个start对象上调用Process。你的例子有第二个process.append的缩进错误,我假设该行根本不应该存在。请注意,启动进程的for循环位于upper for循环内部,因此它将针对您创建的每个进程执行。例如,在第二次循环中,您创建第二个进程,然后再次尝试启动第一个进程。只需将起始代码移出上部for循环。

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, )))

for pr in processes:
    pr.start()

for pr in processes:
    pr.join()

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

答案 2 :(得分:0)

  • 每个进程都应该在每次调用中开始和结束。

示例代码:

from multiprocessing import Process, Queue
cola = Queue()

if __name__ == "__main__":

    while True:
        msgIn = input("Cual es el mensaje: ")

        if "$" in msgIn:
            print("si es un dolar")
            cola.put_nowait(msgIn) 
            
        if "#" in msgIn:
            print ("tamaño conla ", cola.qsize())
            break
    
    for n in range( cola.qsize()):
        proceso = Process(args=(cola,))
        proceso.start()
        print(cola.get( timeout=2))
    
        proceso.join()