0

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?

Daan
  • 349
  • 4
  • 16

1 Answers1

9

This line

threading.Thread(target = f(1)).start()

basically says "start a new thread and execute the result of f(1) in it".

What you want is "start a new thread and execute f with argument 1 in it", which translates to

threading.Thread(target = f, args=(1,)).start()
MSpiller
  • 3,500
  • 2
  • 12
  • 24
  • Note also that `f(1)` returns `None`, which happens to be a valid `target` ("`target` is the callable object to be invoked by the `run()` method. **Defaults to `None`, meaning nothing is called.**" - emphasis mine) – Amadan Sep 24 '18 at 12:59