0

I have a subroutine which I want to run over and over again, but in threads so it runs more times per second. I have tried using threading and using a while loop to run the threads again and again but you can't run one thread more than once.

Here is where I tried to create the threads realCode being my subroutine

checkerThread1 = Thread(name='T1', target=realCode, args=())
checkerThread2 = Thread(name='T2', target=realCode, args=())
checkerThread3 = Thread(name='T3', target=realCode, args=())
checkerThread4 = Thread(name='T4', target=realCode, args=())

while True:
    checkerThread1.start()
    checkerThread2.start()
    checkerThread3.start()
    checkerThread4.start()

I realise that this won't work, any alternatives would be appreciated.

LT_Orange
  • 55
  • 2
  • 6
  • Can you show the code you have tried running? – decorator-factory May 25 '22 at 18:17
  • @decorator-factory Added to the question – LT_Orange May 25 '22 at 18:23
  • If you're starting threads in a `while` loop, soon you'll get hundreds and thousands of threads, which is unlikely to improve the performance :) Did you mean to start just 4 threads and then wait for them to finish? Also, what kind of work does `realCode` do -- CPU-bound or I/O-bound? (it matters because of [the GIL](https://stackoverflow.com/questions/1294382/what-is-the-global-interpreter-lock-gil-in-cpython)) – decorator-factory May 25 '22 at 18:26
  • When I used a while loop it said that a thread can't be started more than once. What I want to happen is the subroutine to run. print the output then run again. realCode is requesting a api and then depending on the response printing something out. But because of the time it takes to make a request and get a response its not hugely fast. – LT_Orange May 25 '22 at 18:33
  • https://stackoverflow.com/questions/29692250/restarting-a-thread-in-python#:~:text=You%20cannot%20restart%20a%20thread.&text=When%20a%20thread%20finished%2C%20its,whole%20new%20set%20of%20everything. – wwii May 25 '22 at 18:45
  • @wwii Thankyou, do you have any suggestions for a alternative method? – LT_Orange May 25 '22 at 18:50
  • Can you refactor `realCode` so that its execution loops? – wwii May 25 '22 at 18:50
  • It's not ideal but probably – LT_Orange May 25 '22 at 18:52
  • After starting, continually check if the threads are alive, if not make and start a new one. `if not checkerThread1.is_alive(): checkerThread1 = Thread(name='T1', target=realCode, args=()); checkerThread1.start()` - something like that – wwii May 25 '22 at 18:55
  • There is also an example in that answer of a *restartable* thread subclass which might be worth using. – wwii May 25 '22 at 19:07
  • @wwii Such continually checking sounds like a bad idea... – Kelly Bundy May 25 '22 at 19:09
  • @KellyBundy ... it was my first thought, it is simple to code. concurent.futures has a `.as_completed()` method that works well but I imagine it is just continually checking the threads. – wwii May 25 '22 at 19:13
  • Please provide enough code so others can better understand or reproduce the problem. – Community May 25 '22 at 20:06

1 Answers1

1

Instead of directly using realCode as thread target, you could use a target that repeatedly calls it:

def repeat(function, *args):
    while True:
        function(*args)

for _ in range(4):
    Thread(target=repeat, args=[realCode]).start()
Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65