I'm experimenting with running code parallel in Python.
My use case is as follows: I have a process that I need to run thousands of times. But due to it's dependency on a download I need to be careful how many I run at the same time (I have a limited number of requests each minute). I therefore would like to always run the function 10 times simultaneously, each time one is finished I would like to start up a new process.
After doing some quick reading the threading module seems to be what I'm looking for. However, some small experiments seem to show that Python does not run the threads in paralell.
When I run the following code
import threading
import time
wait = 0.4
def f(x):
for j in range(1,100):
time.sleep(wait)
print(x)
threading.Thread(target = f(1)).start()
threading.Thread(target = f(2)).start()
I get in the commandline
1
1
1
1
1
Not a singe 2 shows up untill all 100 ones are printed out.
How does this come? In theory f(1) and f(2) should be running simultaneously right?