Python 如何强制中止线程

Python 如何强制中止线程,第1张

Python用sleep停止一个线程的运行,而不影响主线程的运行,案例代码如下:
from threading import import time class MyThread(Thread): def run (self): selfifdo = True; while selfifdo: print 'I am running' timesleep(2) def stop (self): print 'I am stopping it' selfifdo = False; tr = MyThread()trsetDaemon(True)trstart()print 'I will stop it'timesleep(5)trstop()trjoin()

描述 Python time sleep() 函数推迟调用线程的运行,可通过参数secs指秒数,表示进程挂起的时间。

Python有一个名为time的模块,该模块提供了一些有用的功能来处理与时间有关的任务。其中一种常用的函数是sleep()。 sleep()函数将当前线程的执行暂停给定的秒数。 


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13361215.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-21
下一篇 2023-07-21

发表评论

登录后才能评论

评论列表(0条)

保存