-1

Somehow this is frying my brain, i dont know how to get the function to start again after they are done. The goal is to run them at the same time and when theyre finished they should start again:

if __name__ == '__main__':
    current_task += 1
    Thread(target = main).start()
    current_task += 1
    Thread(target = main).start()
    pass
DerApfel0
  • 13
  • 6
  • You have heard of loops? BTW: what is the underlying problem you intend to solve this way? – GhostCat Apr 27 '21 at 11:23
  • Yes i know loops, but need the right one and can come up with it sadly. The problem is that i need to have 2 threads running over and over again when theyre dont, but i dont know how – DerApfel0 Apr 27 '21 at 11:28
  • As said, I think it might beneficial if you explain the underlying problem you intend to solve here, see https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem ... as the one answer implies: what is wrong with just putting the above into a never-ending while loop?! – GhostCat Apr 27 '21 at 11:54
  • Aight so the problem is I can’t find a fitting loop, my loops just start the thread again and again without waiting until the function is finished – DerApfel0 Apr 27 '21 at 12:05
  • See https://stackoverflow.com/questions/11968689/python-multithreading-wait-till-all-threads-finished for example. – GhostCat Apr 27 '21 at 12:10
  • I do indeed lack of basic knowledge ;) – DerApfel0 Apr 27 '21 at 12:21
  • You are welcome. But please understand: you should do proper research prior asking questions. Rest assured: anything you can dream of asking when learning a new language ... has been asked before (here and elsewhere). You should rather invest 60 minutes of using a search engine to find answers ... before spending 5 minutes to write down a question. And when you write questions here, make sure that you really clearly describe the problem you need help with. Your question emphasizes the "start again part", and it tags "loop" ... but what you ... – GhostCat Apr 27 '21 at 12:32
  • really, only cared about was: how to wait for your threads. Thus the two answers you got ... didnt help you much. – GhostCat Apr 27 '21 at 12:32
  • 1
    Well i did some research, maybe im a bit wasted today that i didnt implement it correctly. – DerApfel0 Apr 27 '21 at 12:53
  • No harm done, just a hint how to do it differently in the future ;-) – GhostCat Apr 27 '21 at 13:21

2 Answers2

1

You could use a while loop within main:

import threading
import time


def main():
    while 1:
        print("starting")
        time.sleep(2)
        print("done")


if __name__ == '__main__':
    threading.Thread(target=main).start()

Out:

starting
done
starting
done
starting
...
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
0

You could start a new thread at the end of your main-method:

def main():
  # .. do some stuff ..
  Thread(target=main).start()


if __name__ == '__main__':
    current_task += 1
    Thread(target = main).start()
    current_task += 1
    Thread(target = main).start()
    pass

This way, both threads spawn a new thread with a new execution once they are done.