Reputation: 5
i want to hav a variable number of threads who run at the same time. I tested multiple multithread examples from multiprocessing but they dont run at the same time. To explain it better here an example:
from multiprocessing import Pool
def f(x):
print("a",x)
time.sleep(1)
print("b",x)
if __name__ == '__main__':
with Pool(3) as p:
for i in range(5):
p.map(f, [i])
Result:
a 0
b 0
a 1
b 1
a 2
b 2
Here it does a waits 1 sec and then b, but i want it that all a's get printed first and then b's (That every Thread runs at the same time so that the result looks like this:
a0
a1
a2
b0
b1
b2
Upvotes: 0
Views: 180
Reputation: 57
You mentioned threads but seem to be using processes. The threading module uses threads, the multiprocessing module uses processes. The primary difference is that threads run in the same memory space, while processes have separate memory. If you are looking to use process library. Try using below code snippet.
from multiprocessing import Process
import time
def f(x):
print("a",x)
time.sleep(1)
print("b",x)
if __name__ == '__main__':
for i in range(5):
p = Process(target=f, args=(i,))
p.start()
processes are spawned by creating a Process object and then calling its start() method.
Upvotes: 1
Reputation: 19414
First of all this is not a threads pool, but a processes pool. If you want threads, you need to use multiprocessing.dummy
.
Second, it seems like you misunderstood the map
method. Most importantly, it is blocking. You are calling it with a single numbered list each time - [i]
. So you don't actually use the Pool
's powers. You utilize just one process, wait for it to finish, and move on to the next number. To get the output you want, you should instead do:
if __name__ == '__main__':
with Pool(3) as p:
p.map(f, range(5))
But note that in this case you have a race between the number of processes and the range. If you want all a
s and only then all b
s, try to use Pool(5)
.
Upvotes: 1