1 创建线程
在python3
之后的版本中提供了threading
的模块,该模块中提供了Thread
类可以用来创建线程。
t = threading.Thread(target=None, args=(), kwargs=None)
- target:指定所创建的线程要调度的目标方法,可以传入一个自定函数用于实现功能
- args:以元组的方式,为 target 指定的方法传递参数
- kwargs:以字典的方式,为 target 指定的方法传递参数
threading
模块中提供了几种常用的函数,获取线程信息。
# coding:utf-8
import threading
def action():
print(threading.active_count()) # 当前活动线程的个数
print(threading.enumerate()) # 枚举当前所有活动线程
print(threading.current_thread()) # 正在进行的线程
if __name__ == "__main__":
action()
输出结果:
1
[<_MainThread(MainThread, started 4489174464)>]
<_MainThread(MainThread, started 4489174464)>
下面举一个实际的例子,创建一个新的进程打印语句。
# coding:utf-8
import threading
def action():
print("This is an added thread, number is", threading.current_thread().getName())
if __name__ == "__main__":
t_1 = threading.Thread(target=action) # 实例化Thread类的对象
t_2 = threading.Thread(target=action) # 实例化Thread类的对象
t_1.start() # 运行线程
t_2.start() # 运行线程
输出结果:
This is an added thread, number is Thread-1
This is an added thread, number is Thread-2
2 优先让线程使用CPU资源
由于多线程是按照并发方式执行程序,有可能会出现当前线程的工作未执行结束,就让新的线程加入到工作中,所以Thread
实例化对象的join方法可以优先使用CPU
的资源,保证当前线程工作完成。
# coding:utf-8
import threading
def action():
print("This is an added thread, number is", threading.current_thread().getName())
if __name__ == "__main__":
t_1 = threading.Thread(target=action) # 实例化 Thread 类的对象
t_2 = threading.Thread(target=action) # 实例化 Thread 类的对象
t_1.start() # 运行线程
t_1.join() # 优先让 Thread-1 线程使用CPU资源
t_2.start() # 运行线程
t_2.join() # 优先让 Thread-2 线程使用CPU资源
for i in range(5):
print("This is main thread, number is", threading.current_thread().getName())
输出结果:
This is an added thread, number is Thread-1
This is an added thread, number is Thread-2
This is main thread, number is MainThread
This is main thread, number is MainThread
This is main thread, number is MainThread
This is main thread, number is MainThread
This is main thread, number is MainThread
当一个线程中使用了sleep
的方法,会使得当前线程阻塞,其他线程会马上加入到工作中,使用join
方法会优先在线程中优先使用CPU
资源。
# coding:utf-8
import threading
import time
def action_1():
print("T_1 start:")
time.sleep(5)
print("T_1 finsh")
def action_2():
print("T_2 start:")
print("T_2 finsh")
if __name__ == "__main__":
t_1 = threading.Thread(target=action_1) # 实例化 Thread 类的对象
t_2 = threading.Thread(target=action_2) # 实例化 Thread 类的对象
t_1.start() # 运行线程
# t_1.join() # 优先让 Thread-1 线程使用CPU资源
t_2.start() # 运行线程
# t_2.join() # 优先让 Thread-2 线程使用CPU资源
输出结果:
T_1 start:
T_2 start:
T_2 finsh
T_1 finsh
从结果中可以看出线程Thread-1
执行过程中,由于使用sleep
进入阻塞状态,此时线程Thread-2
加入并执行完工作,最后线程Thread-1
完成工作。
如果对两个线程分别使用join
方法,就可以当一个线程完全执行完成之后,另一个线程才加入工作。
# coding:utf-8
import threading
import time
def action_1():
print("T_1 start:")
time.sleep(1)
print("T_1 finsh")
def action_2():
print("T_2 start:")
print("T_2 finsh")
if __name__ == "__main__":
t_1 = threading.Thread(target=action_1) # 实例化 Thread 类的对象
t_2 = threading.Thread(target=action_2) # 实例化 Thread 类的对象
t_1.start() # 运行线程
t_1.join() # 优先让 Thread-1 线程使用CPU资源
t_2.start() # 运行线程
t_2.join() # 优先让 Thread-2 线程使用CPU资源
输出结果:
T_1 start:
T_1 finsh
T_2 start:
T_2 finsh
网友评论