Python multi-threaded question

Asked 2 years ago, Updated 2 years ago, 114 views

I want each thread to run independently.

One thread and two threads work. When 1th thread ends, I want to make 1th thread work again even if 2th thread is still working.

In the repeat statement

T1=Thread(target=~~~)
T2=Thread(target=~~~)
T1.start()
T2.start()
T1.join()
T2.join()

I wrote it like this. Because of the T2.join() part, the T1 thread waits until the T2 thread ends.

Is there a way?

python multithreading

2022-09-21 18:47

1 Answers

How about using is_alive to join only when T2 is alive?

T1.join()
while True:
    if T2.is_alive():
        T1.start()
    else:
        T2.join()

Like this.


2022-09-21 18:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.