Python - Multiprocessing Error 'cannot start a process twice'

16,336

Solution 1

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

Thank you for all of your reply.

Solution 2

You get the assertion because you call start on a single Process object multiple times. Your example has an indentation error with that second process.append and I'm assuming that the line shouldn't be there at all. 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. 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.

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

Solution 3

  • 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()
Share:
16,336
lloistborn
Author by

lloistborn

Skill : Java, Javascript, Go, Python

Updated on July 01, 2022

Comments

  • lloistborn
    lloistborn almost 2 years

    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)
    

    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
    
    • tdelaney
      tdelaney over 9 years
      which operating system? On Windows, multiprocessing calls must be protected by if __name__=='__main__': to prevent infinite process creation.
    • lloistborn
      lloistborn over 9 years
      Windows 8.1, and i already declare it on my code. The only problem is just the AssertionError that i showed in above, can i get more advise about the error sir? @tdelaney
    • Ethan Furman
      Ethan Furman over 9 years
      The windows problem was fixed in the 2.7 and 3.2+ branches.
    • lloistborn
      lloistborn over 9 years
      Can you attach link about the problem fixed documentation that refer to my issue? @Ethan Furman
    • tdelaney
      tdelaney over 9 years
      @EthanFurman - 3.4 multiprocessing doc still includes the warning: Make sure that the main module can be safely imported by a new Python interpreter without causing unintended side effects (such a starting a new process). Is it out of date?
    • lloistborn
      lloistborn over 9 years
      No, the documentation is good. I just implements something that they made. Please take a look at my question answer update sir @tdelaney
    • Lord Henry Wotton
      Lord Henry Wotton over 9 years
      Can you print out the process ids you are getting when you start each process?
  • lloistborn
    lloistborn over 9 years
    The error is still the same sir. Take a look to my question update please, also i add the full output. Got confused with the error @tdelaney
  • pdaawr
    pdaawr almost 4 years
    Your answer actually helped to resolve my problem, I was starting the same process two times, thanks