Python创建多线程的两种常用方法总结
作者:不想赖床 时间:2023-11-16 16:41:09
经过总结,Python创建多线程主要有如下两种方法:
函数
类
接下来,我们就来揭开多线程的神秘面纱。
1. 用函数创建多线程
在Python3中,Python提供了一个内置模块 threading.Thread,可以很方便地让我们创建多线程。
threading.Thread() 一般接收两个参数:
线程函数名:要放置线程让其后台执行的函数,由我们自已定义,注意不要加();
线程函数的参数:线程函数名所需的参数,以元组的形式传入。若不需要参数,可以不指定。
举个例子
import time
from threading import Thread
# 自定义线程函数。
def target(name="Python"):
for i in range(2):
print("hello", name)
time.sleep(1)
# 创建线程01,不指定参数
thread_01 = Thread(target=target)
# 启动线程01
thread_01.start()
# 创建线程02,指定参数,注意逗号
thread_02 = Thread(target=target, args=("MING",))
# 启动线程02
thread_02.start()
可以看到输出
hello Python
hello MING
hello Python
hello MING
2. 用类创建多线程
相比较函数而言,使用类创建线程,会比较麻烦一点。
首先,我们要自定义一个类,对于这个类有两点要求,
必须继承 threading.Thread 这个父类;
必须复写 run 方法。
这里的 run 方法,和我们上面线程函数的性质是一样的,可以写我们的业务逻辑程序。在 start() 后将会调用。
来看一下例子 为了方便对比,run函数我复用上面的main。
import time
from threading import Thread
class MyThread(Thread):
def __init__(self, type="Python"):
# 注意:super().__init__() 必须写
# 且最好写在第一行
super().__init__()
self.type=type
def run(self):
for i in range(2):
print("hello", self.type)
time.sleep(1)
if __name__ == '__main__':
# 创建线程01,不指定参数
thread_01 = MyThread()
# 创建线程02,指定参数
thread_02 = MyThread("MING")
thread_01.start()
thread_02.start()
当然结果也是一样的。
hello Python
hello MING
hello Python
hello MING
3. 线程对象的方法
上面介绍了当前 Python 中创建线程两种主要方法。
创建线程是件很容易的事,但要想用好线程,还需要学习线程对象的几个函数。
经过我的总结,大约常用的方法有如下这些:
# 如上所述,创建一个线程
t=Thread(target=func)
# 启动子线程
t.start()
# 阻塞子线程,待子线程结束后,再往下执行
t.join()
# 判断线程是否在执行状态,在执行返回True,否则返回False
t.is_alive()
t.isAlive()
# 设置线程是否随主线程退出而退出,默认为False
t.daemon = True
t.daemon = False
# 设置线程名
t.name = "My-Thread"
来源:https://juejin.cn/post/7231552045933002807
标签:Python,创建,多线程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python命令行参数解析模块getopt使用实例
2021-02-20 08:53:48
Python中flask框架跨域问题的解决方法
2023-10-23 11:10:52
![](https://img.aspxhome.com/file/2023/0/121000_0s.png)
Dreamweaver小技巧:超高速下载图像
2009-07-14 21:59:00
![](https://img.aspxhome.com/file/UploadPic/20072/20072311307259s.jpg)
sqlserver、mysql获取连接字符串步骤
2024-01-22 00:49:33
Go并发编程中sync/errGroup的使用
2024-02-05 04:40:56
matplotlib.pyplot画图 图片的二进制流的获取方法
2021-03-25 09:38:00
JS 类型转换常见方法小结
2024-04-17 09:49:17
Python工程师面试题 与Python基础语法相关
2021-07-08 16:20:50
获取body标签的两种方法
2024-05-08 09:39:26
详解vue组件开发脚手架
2024-05-09 15:15:34
![](https://img.aspxhome.com/file/2023/4/126514_0s.jpg)
Python解决多进程间访问效率低的方法总结
2023-11-25 11:57:43
oracle行转列方法集合汇总(推荐!)
2024-01-12 19:42:14
![](https://img.aspxhome.com/file/2023/1/65671_0s.png)
python 利用turtle库绘制笑脸和哭脸的例子
2022-01-16 08:35:30
![](https://img.aspxhome.com/file/2023/7/86157_0s.jpg)
GO语言make()分配用法实例
2024-01-30 00:54:14
将Django项目部署到CentOs服务器中
2021-07-30 20:11:11
![](https://img.aspxhome.com/file/2023/0/114530_0s.png)
goland设置控制台折叠效果
2024-05-22 10:13:58
![](https://img.aspxhome.com/file/2023/6/123916_0s.png)
SQL Group By分组后如何选取每组最新的一条数据
2024-01-18 21:50:51
![](https://img.aspxhome.com/file/2023/7/121237_0s.png)
asp如何显示全部的环境变量?
2010-06-08 09:34:00
Python函数进阶之迭代器的原理与使用详解
2023-03-28 09:02:01
解决VUE自定义拖拽指令时 onmouseup 与 click事件冲突问题
2024-05-10 14:15:54
![](https://img.aspxhome.com/file/2023/1/126491_0s.jpg)