0

Code:

from threading import Thread
import time

def main():
    print([threadID])
    time.sleep(5)
    pass

if __name__ == '__main__':
    threadID = 0
    while threadID < 5:
        main()
        threadID +=1

Currently, it runs the first thread, then once it is finished it starts the next.

How can I get all threads to start at the same time?

Piers Thomas
  • 307
  • 1
  • 2
  • 16

1 Answers1

0

I feel a little stupid but was never actually calling threading...

Code:

from threading import Thread
import time

def main():
    print([threadID])
    time.sleep(5)
    pass

if __name__ == '__main__':
    threadID = 0
    while threadID < 5:
        t = Thread(target=main)
        t.start()
        threadID +=1
Piers Thomas
  • 307
  • 1
  • 2
  • 16
  • While what you're doing here might work for trivial use cases, in general it's a better idea to let Python manage multiple threads when you want to use them like this. I'm linking to a [particular example in the doc](https://docs.python.org/3/library/concurrent.futures.html#threadpoolexecutor-example); while reading from the start is important, in my opinion this is the most useful practical example in there. `multiprocessing.pool` also provides `ThreadPool`, though the documentation is... not really there. – kungphu Oct 01 '18 at 02:04