0

I have read Is there an easy way in Python to wait until certain condition is true? and a couple other posts, but I don't think any of them satisfy my specific problem. I apologize if I missed one I just am not sure what to search outside of what I already have.

I have many threads doing some sort of computing. I need them to print in order of creation. My current solution was:

while thread_num != current_print_num:
    pass
print(data)
current_print_num += 1

I don't believe this is the most efficient way of going about it. Is there a way to have a thread stop executing for a while after checking the condition I set in the while loop? I understand I can have it sleep, but I would like to not have to set an amount of time for it to wait. Ideally it would awake when current_print_num == thread_num.

A good solution would be to have the threads communicate with each other on when the previous one is finished. Is there a good way to do that?

  • Take a look at this problem/my answer here. I think something like this could work for you. https://stackoverflow.com/questions/69331788/synchronize-asyncio-queue/69331930#69331930 – Andrew-Harelson Oct 04 '21 at 04:55
  • If you want to wait for a thread to finish, [`.join()`](https://docs.python.org/3/library/threading.html#threading.Thread.join) the thread. – Klaus D. Oct 04 '21 at 05:10

0 Answers0