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
Calling the start() function on a terminated process will result in an AssertionError indicating that the process can only be started once. Instead, to restart a process in Python, you must create a new instance of the process with the same configuration and then call the start() function.
The argument for multiprocessing. Pool() is the number of processes to create in the pool. If omitted, Python will make it equal to the number of cores you have in your computer. We use the apply_async() function to pass the arguments to the function cube in a list comprehension.
multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads.
Python multiprocessing Pool can be used for parallel execution of a function across multiple input values, distributing the input data across processes (data parallelism).
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With