1 Star 0 Fork 1

aliao/stdpython

forked from pish7/stdpython 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
threading_05.py 781 Bytes
一键复制 编辑 原始数据 按行查看 历史
pish7 提交于 2023-10-05 21:56 +08:00 . python
import threading
import time
# 线程类
# 使用一个类来管理线程,但这个类却不继承自 threadig.Thread
# 这样的好处是这个类不再依赖于 threading.Thread 模块,而是独立地定义一段可并行运行的代码,可用于 multiprocessing 等模块
class ThreadGo:
def __init__(self, name="", *args, **kwargs):
self.args = args
self.kwargs = kwargs
self.name = name
self.go = True
#终止线程
def terminate(self):
self.go = False
#线程函数
def run(self):
while self.go:
print("go")
time.sleep(.5)
# 创建线程(使用一个对象方法)
tg = ThreadGo()
th = threading.Thread(target=tg.run, name=tg.name)
# 启动线程
th.start()
time.sleep(5)
tg.terminate()
th.join()
print("exit...")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/aliaodc/stdpython.git
git@gitee.com:aliaodc/stdpython.git
aliaodc
stdpython
stdpython
master

搜索帮助