【说站】python threading实现线程的过程
2023-06-13 09:13:22 时间
python threading实现线程的过程
过程说明
1、从Thread类构成子类。覆盖方法根据需要提供参数。
2、重写线程的业务逻辑。
3、新的Thread子类一旦被定义,就必须对其进行实例化,以启动新的线程。
然后,调用方法启动它。最后,它将调用方法来执行业务逻辑。
实例
#Python 多线程示例打印当前日期。
#1. 使用 threading.Thread 类定义子类。
#2. 实例化子类并触发线程。
import threading
import datetime
class myThread (threading.Thread):
def __init__(self, name, counter):
threading.Thread.__init__(self)
self.threadID = counter
self.name = name
self.counter = counter
def run(self):
print("\nStarting " + self.name)
print_date(self.name, self.counter)
print("Exiting " + self.name)
def print_date(threadName, counter):
datefields = []
today = datetime.date.today()
datefields.append(today)
print("{}[{}]: {}".format( threadName, counter, datefields[0] ))
# 创建新线程
thread1 = myThread("Thread", 1)
thread2 = myThread("Thread", 2)
# 启动新线程
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print("\nExiting the Program!!!")
以上就是python threading实现线程的过程,希望对大家有所帮助。
相关文章
- Python安装第三方库(离线+在线)
- Python升级之路( Lv15 ) 并发编程三剑客: 进程, 线程与协程
- python监控网页内容变化_使用Python监控文件内容变化代码实例
- python监控网站更新_Python 通过网站search功能监控网站内容更新[通俗易懂]
- 【Python】python文件打开方式详解——a、a+、r+、w+、rb、rt区别[通俗易懂]
- python selenium 实现快手自动评论
- 【说站】python中ThreadPoolExecutor如何使用
- 【说站】python threading线程同步如何实现
- Python实现自动回复_python 微信机器人
- python分析人口出生率代码_国家统计局居然也能用的上Python?人口数据Python脚本了解一下?…[通俗易懂]
- Python生成随机数列表_numpy产生指定范围的随机数
- python分段线性插值_Python实现分段线性插值
- python定义函数求和_Python定义函数实现累计求和操作
- python使用opencv如何保存图片_OpenCV Python 保存图片
- opencv(4.5.3)-python(十八)--轮廓线入门
- python装饰器进阶指南
- python-数据库编程-如何在Python中连接到数据库
- python-Python与MongoDB数据库-MongoDB数据库的基本知识
- MySQL与Python搭配,实现数据库操作。(mysql-python)
- 安装Python MySQL驱动之快速指南(python安装mysql驱动)
- 在Linux上学习Python——你的编程之路(linux学python)
- Python实现Oracle数据库连接(python连接oracle数据库)
- python用Redis与Python实现大数据收集与分析(redis 联合)
- python线程池的实现实例