JOIN usage in Python multi-thread

tags: Python development  python

Concept: In Python Multi-threaded programming, the role of join method is thread synchronization
The following is divided into five different forms of Join in multi-threaded programming

The first: Python multi-thread default (set thread setdaemon (false)), after the main thread performs your own task, then quit, at which time the sub-line continues to perform his own task until the end of your own task

import threading, time
def doWaiting1():
    print('start waiting1: ' + time.strftime('%H:%M:%S') + "\n")
    time.sleep(3)
    print("Thread 1 is ordered to report")
    print('stop waiting1: ' + time.strftime('%H:%M:%S') + "\n")
def doWaiting2():
    print('start waiting2: ' + time.strftime('%H:%M:%S') + "\n")
    time.sleep(8)
    print("Thread 2 is ordered to report")
    print('stop waiting2: ', time.strftime('%H:%M:%S') + "\n")

tsk = []

# Create and turn on thread 1
thread1 = threading.Thread(target = doWaiting1)
thread1.start()
tsk.append(thread1)

# Create and turn on thread 2
thread2 = threading.Thread(target = doWaiting2)
thread2.start()
tsk.append(thread2)

# Timing program
print('start join: ' + time.strftime('%H:%M:%S')  )
print('end join: ' + time.strftime('%H:%M:%S') )

operation result

start waiting1: 20:03:30
start waiting2: 20:03:30

start join: 20:03:30
end join: 20:03:30

 Thread1Dedication
stop waiting1: 20:03:33

 Thread2Dedication
stop waiting2:  20:03:38

in conclusion:
1. The timer is the main thread. The entire main thread will enter the timing module after turning on thread 1 and thread 2, and the main thread ends
2, the main thread ends, but does not affect the operation of thread 1 and thread 2, so the latter thread 1 and thread 2 still runs, so the entire program is completely over

Second: Turning the ThetDaemon (TRUE) of the thread, set the sub-thread to the daemon, the main program ends, the subroutine is full end function

import threading, time
def doWaiting1():
    print('start waiting1: ' + time.strftime('%H:%M:%S') + "\n")
    time.sleep(3)
    print("Thread 1 is ordered to report")
    print('stop waiting1: ' + time.strftime('%H:%M:%S') + "\n")
def doWaiting2():
    print('start waiting2: ' + time.strftime('%H:%M:%S') + "\n")
    time.sleep(8)
    print("Thread 2 is ordered to report")
    print('stop waiting2: ', time.strftime('%H:%M:%S') + "\n")

tsk = []

# Create and turn on thread 1
thread1 = threading.Thread(target = doWaiting1)
thread1.setDaemon(True)
thread1.start()
tsk.append(thread1)

# Create and turn on thread 2
thread2 = threading.Thread(target = doWaiting2)
thread2.setDaemon(True)
thread2.start()
tsk.append(thread2)

print('start join: ' + time.strftime('%H:%M:%S')  )
print('end join: ' + time.strftime('%H:%M:%S') )

operation result:

start waiting1: 20:10:04
start waiting2: 20:10:04

start join: 20:10:04
end join: 20:10:04

in conclusion:
1. After the main thread is over, whether it is not completed whether the child thread 1, 2 is completed, it is no longer to continue to run.

The third: joining the Join method, when the program is not given the daemon, the main program will always wait until the subscriber is completed.

import threading, time
def doWaiting1():
    print('start waiting1: ' + time.strftime('%H:%M:%S') + "\n")
    time.sleep(3)
    print("Thread 1 is ordered to report")
    print('stop waiting1: ' + time.strftime('%H:%M:%S') + "\n")
def doWaiting2():
    print('start waiting2: ' + time.strftime('%H:%M:%S') + "\n")
    time.sleep(8)
    print("Thread 2 is ordered to report")
    print('stop waiting2: ', time.strftime('%H:%M:%S') + "\n")

tsk = []

# Create and turn on thread 1
thread1 = threading.Thread(target = doWaiting1)
thread1.start()
tsk.append(thread1)

# Create and turn on thread 2
thread2 = threading.Thread(target = doWaiting2)
thread2.start()
tsk.append(thread2)

print('start join: ' + time.strftime('%H:%M:%S')  )
for t in tsk:
    print('% s thread is "%t)
    t.join()
print('end join: ' + time.strftime('%H:%M:%S') )

operation result:

start waiting1: 20:14:35
start waiting2: 20:14:35

start join: 20:14:35

<Thread(Thread-1, started 19648)>Thread
 Thread1Dedication
stop waiting1: 20:14:38

<Thread(Thread-2, started 24056)>Thread
 Thread2Dedication
stop waiting2:  20:14:43

end join: 20:14:43

in conclusion:
1. Use the join function, the main thread will be blocked, waiting for the thread running to be used in the Join method.
2, start join is in 35 seconds, Stop Waiting1 is 38 seconds, just Sleep for 3 seconds, Stop Waiting2 is 43 seconds, just Sleep for 8 seconds, this also shows that threads 1 and 2 are basically running, but due to execution The time consumes is inconsistent, so the time used to block the time is not the same, and the final end Join time is the last thread, and the entire program will stop in 43 seconds.
3, put all the threads into a list, use the JOIN method to use the JOIN method to ensure that all sub-threads can be run, the main line is exited

The fourth: universal does not set the daemon, but set the timeout value for Join, and judge that the sub-loader has not been completed, then the main thread is no longer waiting

import threading, time
def doWaiting1():
    print('start waiting1: ' + time.strftime('%H:%M:%S') + "\n")
    time.sleep(2)
    print("Thread 1 is ordered to report")
    print('stop waiting1: ' + time.strftime('%H:%M:%S') + "\n")
def doWaiting2():
    print('start waiting2: ' + time.strftime('%H:%M:%S') + "\n")
    time.sleep(8)
    print("Thread 2 is ordered to report")
    print('stop waiting2: ', time.strftime('%H:%M:%S') + "\n")

tsk = []

# Create and turn on thread 1
thread1 = threading.Thread(target = doWaiting1)
thread1.start()
tsk.append(thread1)

# Create and turn on thread 2
thread2 = threading.Thread(target = doWaiting2)
thread2.start()
tsk.append(thread2)

print('start join: ' + time.strftime('%H:%M:%S')  )
for t in tsk:
    print("Start:"+time.strftime('%H:%M:%S'))
    print('% s thread is "%t)
    t.join(5)
    print("Finish:" + time.strftime('%H:%M:%S'))
print('end join: ' + time.strftime('%H:%M:%S') )

operation result:

start waiting1: 21:14:25
start waiting2: 21:14:25

start join: 21:14:25

 Start:21:14:25
<Thread(Thread-1, started 22348)>Thread
 Thread1Dedication
stop waiting1: 21:14:27
 Finish:21:14:27

 Start:21:14:27
<Thread(Thread-2, started 13164)>Thread
 Finish:21:14:32

end join: 21:14:32

 Thread2Dedication
stop waiting2:  21:14:33

in conclusion:
1. After setting the wait time, after waiting time, the main thread is terminated, but does not affect the sub-thread to continue to run, and the subscriber thread is running, the entire program is terminated.
2, all threads available waiting time Timeout_total ≤ TimeoutNumber of threads
3, although Timeout is set to 5s, but thread 1 only needs 2s, so the loop is from the beginning to the end, just consumes 2s (21:14:27 - 21:14:25), it will enter the second time. The second waiting can still be assigned 5s (21:14:32 - 21:14:27), so the total waiting is time 2 + 5 = 7s, but the thread 2 is running to run 8S, and 8s is from 21:14:25 start, the end time is 21:14:33, because Join Waiting time is over the main program, but does not affect the thread 2 continues to run, so after End Join, thread 2 still outputs the report result (Yes Because there is no opening of the daemon)
4, individual articles explaining Join this time is: the main thread will wait for the Timeout accumulation and the number of threads, this statement is inaccurate, and the reasoning can be derived, not necessarily the number of threads.
Timeout "So much time, but ≤" thread number * Timeout ",

The fifth: On the basis of four, open the daemon, the sub-thread that is out of time, the sub-thread will be directly terminated.

import threading, time
def doWaiting1():
    print('start waiting1: ' + time.strftime('%H:%M:%S') + "\n")
    time.sleep(2)
    print("Thread 1 is ordered to report")
    print('stop waiting1: ' + time.strftime('%H:%M:%S') + "\n")
def doWaiting2():
    print('start waiting2: ' + time.strftime('%H:%M:%S') + "\n")
    time.sleep(8)
    print("Thread 2 is ordered to report")
    print('stop waiting2: ', time.strftime('%H:%M:%S') + "\n")

tsk = []

# Create and turn on thread 1
thread1 = threading.Thread(target = doWaiting1)
thread1.setDaemon(True)
thread1.start()
tsk.append(thread1)

# Create and turn on thread 2
thread2 = threading.Thread(target = doWaiting2)
thread2.setDaemon(True)
thread2.start()
tsk.append(thread2)

print('start join: ' + time.strftime('%H:%M:%S')  )
for t in tsk:
    print("Start:"+time.strftime('%H:%M:%S'))
    print('% s thread is "%t)
    t.join(5)
    print("Finish:" + time.strftime('%H:%M:%S'))
print('end join: ' + time.strftime('%H:%M:%S') )

operation result:

start waiting1: 21:24:14
start waiting2: 21:24:14

start join: 21:24:14

 Start:21:24:14
<Thread(Thread-1, started daemon 9060)>Thread
 Thread1Dedication
stop waiting1: 21:24:16
 Finish:21:24:16

 Start:21:24:16
<Thread(Thread-2, started daemon 13912)>Thread
 Finish:21:24:21

end join: 21:24:21

in conclusion:
1, compared to the fourth, the main thread runs to End Join after the timeout, the sub-thread 2 has been terminated to stop operation.

Intelligent Recommendation

Python multi-thread: THREAD class usage

We want to create a Thread object, then let them run, each THRead object represents a thread, in each thread we can make the program handle different tasks, this is multi-threaded programming. There a...

Java Thread join() usage

In the Java Thread, the join() method mainly asks the code that calls the change method to complete the contents of the run method after executing the join() method. Example: If the t1.join() statemen...

Thread join usage

package api.day08; public class ThreadJoinDemo { }...

More Recommendation

thread the join () usage

Some time ago to participate in the interview, the interviewer was asked to join a thread in usage method, that would be a little abrupt own answer it, my answer to the interviewer does not seem very ...

Java Thread join () usage

Java Thread temporarily stuck in, join () method is to let the main thread calls this method executes the run () when, etc. run () execution is completed, the main thread and then call execute join ()...

Join thread usage example

1. To achieve Runnbale interface  ...

join thread in the () usage

1: First of all, said method Thread Thread join in: As the name suggests is to add something to the thread; this method can be used to temporarily join a thread, a thread in the operation process, if ...

Usage of Java thread join ()

The role of join () method is to wait for this thread to end; In other words, the t.join () method blocks the calling thread from entering the TIMED_WAITING state until the thread t completes, and the...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top