tags: python notes Multithreading python
import threading
import time
lock = threading.Lock()
threads = []
class MyThread(threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print("Start thread" + self.name)
lock.acquire()
# Open lock
print_time(self.name, self.counter, 3)
# Release the lock
lock.release()
def print_time(ThreadName, delay, counter):
while counter:
time.sleep(delay)
print("%s : %s " % (ThreadName, time.ctime(time.time())))
counter -= 1
def _main():
# Create thread
thread1 = MyThread(1, "Thread-1", 1)
thread2 = MyThread(2, "Thread-2", 2)
thread1.start()
thread2.start()
print('Exit the main thread')
When executing the above program, you will find that after the main thread exits, the child threads are still executing, and the process ends after all the child threads are executed.
threads.append(thread1)
threads.append(thread2)
for i in threads:
i.join()
print('Exit the main thread')
Insert join() after start to achieve thread synchronization, so that the main thread can enter the blocking state before the execution of the child thread is completed. After the execution of the child thread is completed, the main thread will continue to execute.
The output result becomes
Start thread Thread-1
Start thread Thread-2
…(sub-thread printing result)
Exit the main thread
Reference blog: The first case (not using join): In this case, thread t1 and thread t2 compete fairly The second case (the role of join): Thread t1 and thread t2 make the original contention fair and ...
Few facts 1 python default parameters after the thread is created, regardless of whether the main thread is executed, will wait for the child thread to complete the execution before exiting, with or w...
Test code...
First briefly introduce daemon thread: Work server daemon thread-like, as long as there is no request sent by the client, has been running and remains idle, much like the background. threading module ...
Look at the setDaemon () method of the thread The above output is: We modify the code: The output of the program is: It can be seen that the setDaemon () method is to determine if the sub-thread is en...
join() Create a thread (thread name THREAD-1) using Thread, which performs the above TEST () function. IFname == ‘main': The following is the main thread, the main thread will continue to perfor...
Multi-threaded calls can greatly improve the efficiency of the program, but the use of threads on the one hand will reduce readability and on the other hand bring randomness to the operation of the co...
Multi -threading is similar to performing multiple different programs at the same time. Multi -threading has the following advantages: Using threads can put the tasks in the procedure for a long time ...
First understand what multithreading and processes are Process: It is an execution process, a dynamic concept --->Memory will be allocated Thread: a unit of the process, the thread is the smallest ...