Python中如何在一段时间后停止程序

用到threading的Timer,也类似单片机那样子,在中断程序中再重置定时器,设置中断,python实例代码如下:import threading import time def change_user():print('这是中断,切换账号')t = threading.Timer(3, change_user)t.start()每过3秒切换一次账号 t = threading.Timer(3, change_user)t.start(...
Python中如何在一段时间后停止程序
#python 2.7
import time #导入 time类
start=time.clock()
def func(a,b):
while True:
end=time.clock ()
if int(end-start)==10:
print('Warning: Timeout!!'*5)
break
a=a+b

print a
func(1,2)

主要思路:开始时间-当前时间=10则停止运行并输出时间到了(注意缩进)2013-04-04
用到threading的Timer,也类似单片机那样子,在中断程序中再重置定时器,设置中断,python实例代码如下:
import threading
import time
def change_user():
print('这是中断,切换账号')
t = threading.Timer(3, change_user)
t.start()
#每过3秒切换一次账号
t = threading.Timer(3, change_user)
t.start()
while True:
print('我在爬数据')
time.sleep(1)

扩展资料
有时当一个条件成立的情况下,需要终止程序,可以使用sys.exit()退出程序。sys.exit()会引发一个异常:
1、如果这个异常没有被捕获,那么python编译器将会退出,后面的程序将不会执行。
2、如果这个异常被捕获(try...except...finally),捕获这个异常可以做一些额外的清理工作,后面的程序还会继续执行。
注:0为正常退出,其他数值(1-127)为不正常,可抛异常事件供捕获。另一种终止程序的方法os._exit()
一般情况下使用sys.exit()即可,一般在fork出来的子进程中使用os._exit()
采用sys.exit(0)正常终止程序,程序终止后shell运行不受影响。
采用os._exit(0)关闭整个shell,调用sys._exit(0)后整个shell都重启了(RESTART Shell)。
2020-05-12
建议你另起一个timer的线程,计算时间并输出警告。送你一段定时器timer的代码。
使用前先做一个简单试验:
import threading def sayhello():
print "hello world"
global t #Notice: use global variable!
t = threading.Timer(5.0, sayhello)
t.start()

t = threading.Timer(5.0, sayhello)
t.start()

运行结果如下
>python hello.py
hello world
hello world
hello world
下面是定时器类的实现:
class Timer(threading.Thread): """
very simple but useless timer.
"""
def __init__(self, seconds):
self.runTime = seconds
threading.Thread.__init__(self)
def run(self):
time.sleep(self.runTime)
print "Buzzzz!! Time's up!"

class CountDownTimer(Timer):
"""
a timer that can counts down the seconds.
"""
def run(self):
counter = self.runTime
for sec in range(self.runTime):
print counter
time.sleep(1.0)
counter -= 1
print "Done"

class CountDownExec(CountDownTimer):
"""
a timer that execute an action at the end of the timer run.
"""
def __init__(self, seconds, action, args=[]):
self.args = args
self.action = action
CountDownTimer.__init__(self, seconds)
def run(self):
CountDownTimer.run(self)
self.action(self.args)

def myAction(args=[]):
print "Performing my action with args:"
print args

if __name__ == "__main__": t = CountDownExec(3, myAction, ["hello", "world"])
t.start()2013-04-04
把程序单开成一个线程或者进程,最好开进程,python的多线程。。。不太好用,然后用time模块等待几秒,之后直接结束进程就行。2018-08-13
用装饰器能做到。不过有点技巧2013-04-04
mengvlog 阅读 8 次 更新于 2025-07-18 19:14:06 我来答关注问题0
  •  好学者百科 Python中如何在一段时间后停止程序

    while True:print('我在爬数据')time.sleep(1)

  •  深空见闻 python实现每隔两小时重启

    Python实现每隔两小时重启,可以通过以下几种方法实现:使用外部监控工具:Supervisor或Monit:这些工具可以监控Python程序的状态,并在程序运行一段时间后(或崩溃时)自动重启。通过配置autorestart=unexpected(在程序意外退出时重启)或者结合时间触发器,可以实现定时重启。这种方法适用于需要稳定监控和自动重启的...

  • Python中如何在一段时间后停止程序用到threading的Timer,也类似单片机那样子,在中断程序中再重置定时器,设置中断,python实例代码如下:importthreading importtime defchange_user():?print('这是中断,切换账号')?t=threading.Timer(3,change_user)?t.start()每过3秒切换一次账号 t=threading.Timer(3...

  •  猪八戒网 python设置多少秒关闭?

    运行完后屏幕直接白屏(图片就不放了),常规方法例如关闭、ALT+F4、任务管理器都是没办法关掉的。想要解决可以直接重启或者采取一些特殊方法 完成之后,在终端(或cmd)里运行:pyinstaller-F-w带路径的py文件名.py Python中如何在一段时间后停止程序用到threading的Timer,也类似单片机那样子,在中断程序...

  •  阿暄生活 ontimer是什么函数

    当定时器到期时,它会发送一个 WM_TIMER 消息到指定的窗口过程(Window Procedure),从而触发一个事件或执行一个回调函数。用途:这个函数通常用于在一段时间后执行某个任务,而不需要持续占用程序的主线程。这有助于保持程序的响应性,特别是在需要执行耗时操作时。2. Python 中的 ontimer 函数(以 ...

檬味博客在线解答立即免费咨询

报错相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部