import multiprocessing as mlp
class Test(mlp.Process):
def run(self):
if self.name == "Test-2":
import time
time.sleep(4)
print self.name
jobs = []
for i in range(5):
m = Test()
jobs.append(m)
m.run()
for i in jobs:
i.join()
In the above code, output is always:
Test-1
Test-2
Test-3
Test-4
Test-5
And these processes spawn only one after another. Like Test-3 spans only after Test-2 is completed. Shouldn’t they happen parallely???
And also, i.join() throws error: AssertionError: can only join a started process
What wrong am I doing???
Thanks…
You never started the process. The
runmethod is intended for the subclass to implement, which you correctly did, but thestartmethod is the one you should call to start the process (and automatically executerun()in the other process instance).In other words, call
m.start()instead ofm.run().