Python is fast and beautiful [v1.0.0] [control thread]

JOIN thread

The Thread class provides a join () function, which is used to make a thread wait for another thread to complete execution. When thread A calls thread B's join () function during the execution of the program, thread A will be blocked until thread B The execution is complete.
The design of this method is mainly used to split the big problem into several small problems and assign threads to each small problem. When all the small problems are solved, the main thread is called to further operate

import threading

# 定义action函数准备作为线程执行体使用
def action(max):
    for i in range(max):
        print(threading.current_thread().name + " " + str(i))
  
# 启动子线程
threading.Thread(target=action, args=(100,), name="新线程").start()
for i in range(100):
    if i == 20:
        jt = threading.Thread(target=action, args=(100,), name="被Join的线程")
        jt.start()
        # 主线程调用了jt线程的join()方法,主线程
        # 必须等jt执行结束才会向下执行
        jt.join()
    print(threading.current_thread().name + " " + str(i))
  • This program actually has a total of 3 threads. In addition to the main thread, a thread named "new thread" is defined, and then a thread named "thread joined" is defined.
  • After starting the program, the "new thread" is executed in parallel with the main thread
  • When the loop variable i = 20 of the main thread starts the "thread joined", the thread will not execute in parallel with the main thread because the main thread calls its join () function, so the main thread waits and is in a blocked state
  • Until the "thread
    joined " is executed, the main thread continues to execute the join () function can specify a timeout parameter, that is, the wait time, the unit is seconds, if the joined thread has not ended within the timeout time, then no longer continue waiting

Background thread

The background thread is also called a daemon thread, which is mainly used to provide services for other threads. The background thread has a feature that if all foreground threads die, the background thread will automatically die.

import threading

# 定义后台线程的线程执行体与普通线程没有任何区别
def action(max):
    for i in range(max):
        print(threading.current_thread().name + "  " + str(i))
t = threading.Thread(target=action, args=(100,), name='后台线程')
# 将此线程设置成后台线程
# 也可在创建Thread对象时通过daemon参数将其设为后台线程
t.daemon = True
# 启动后台线程
t.start()
for i in range(10):
    print(threading.current_thread().name + "  " + str(i))
# -----程序执行到此处,前台线程(主线程)结束------
# 后台线程也应该随之结束
  • There are two ways to create a background thread, one is to set the daemon attribute of the thread to True, and the other is that the child thread created by the background thread is the background thread by default, and the child thread created by the foreground thread is also the foreground thread
  • Set the background thread, it must be set before the thread is started, otherwise RuntimeError will be reported

Thread sleep

import time

for i in range(10):
    print("当前时间: %s" % time.ctime())
    # 调用sleep()函数让当前线程暂停1s
    time.sleep(1)
Published 215 original articles · praised 183 · 100,000+ views

Guess you like

Origin blog.csdn.net/dawei_yang000000/article/details/105609929