简体   繁体   English

Python - 多处理错误“无法启动一个进程两次”

[英]Python - Multiprocessing Error 'cannot start a process twice'

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.我尝试使用 Python 中的multiprocessing包开发算法,我从互联网上学习了一些教程,并尝试使用此包开发算法。 After looking around and try my 'hello world' using Process , Queue and Pool , i try to implement the Queue on this code在环顾四周并使用ProcessQueuePool尝试我的“hello world”之后,我尝试在此代码上实现 Queue

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)

But, got error like:但是,得到如下错误:

AssertionError: Cannot start a process twice

Can somebody please help with this issue有人可以帮忙解决这个问题吗

full output:完整输出:

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

You get the assertion because you call start on a single Process object multiple times.您得到断言是因为您多次调用单个Process对象的start Your example has an indentation error with that second process.append and I'm assuming that the line shouldn't be there at all.您的示例在第二个process.append存在缩进错误,我假设该行根本不应该存在。 Notice that the for loop where you start the processes is inside the upper for loop so it is executed for every process you create.请注意,启动进程的 for 循环位于上层 for 循环内,因此它为您创建的每个进程执行。 On the second time through the loop, for example, you create the second process and then try to start the first process again.例如,在第二次循环时,您创建第二个进程,然后尝试再次启动第一个进程。 Just move the start code out of the upper for loop.只需将起始代码移出上层 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())

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

in case that k = 3 i need 3 Process that working on my app.如果k = 3我需要 3 个处理我的应用程序的进程。 For the first loop i run the Process twice , so i just start the Process each iteration.对于第一个循环,我运行 Process两次,所以我只是在每次迭代时启动 Process。 So, i delete this code所以,我删除了这段代码

for pr in processes:
    pr.start()

for pr in processes:
    pr.join()

Thank you for all of your reply.谢谢大家的回复。

  • Each process should start and die in each call.每个进程都应该在每次调用中开始和结束。

Example code:示例代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM