栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python执行超时退出

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python执行超时退出

eventlet
import time
import eventlet  #导入eventlet这个模块
eventlet.monkey_patch()   #必须加这条代码
with eventlet.Timeout(2,False):   #设置超时时间为2秒
   print '这条语句正常执行'
   time.sleep(4)
   print '没有跳过这条输出'
print '跳过了输出'

问题:
1、针对子进程无法跳出

timeout_decorator
from tqdm import trange
import sys
import time
import timeout_decorator

@timeout_decorator.timeout(int(sys.argv[2]))
def test():
    if sys.argv[1] == '--timeout':
        for i in trange(3):
            time.sleep(1)
            print ('>>> {} seconds passed.'.format(i+1))
    return 0

if __name__ == '__main__':
    try:
        test()
    except Exception as e:
        print ('Timeout Error Catched!')
        print (e)
    print ("Timeout Task Ended!")

问题:
1、报错signal only works in main thread
在默认情况下,timeout-decorator使用信号来限制给定函数的执行时间。

如果您的函数不在主线程中执行(例如,如果它是Web应用程序的工作线程),则此配置不起作用。在这种情况下使用多进程来作为超时替代策略。要使用它,只需将use_signals = False传递给超时装饰器函数:

import time
import timeout_decorator

@timeout_decorator.timeout(5, use_signals=False)
def mytest():
    print "Start"
    for i in range(1,10):
        time.sleep(1)
        print("{} seconds have passed".format(i))

if __name__ == '__main__':
    mytest()

2、多线程使用会有问题,在多处理策略超时的情况下,您的函数不会返回无法被pickle的对象,否则它将在主进程和子进程之间进行编组时失败。

signal
import signal

class TimeOutException(Exception):
    pass


def handle(signum, frame):
    raise TimeOutException("运行超时!")


def set_timeout(timeout, callback):
    def wrapper(func):
        def inner(*args, **kwargs):
            try:
                signal.signal(signal.SIGALRM, handle)
                signal.alarm(timeout)  # 开启闹钟信号
                rs = func(*args, **kwargs)
                signal.alarm(0)  # 关闭闹钟信号
                return rs
            except TimeOutException as e:
                callback()

        return inner

    return wrapper


def process_time():
    print("超时了")


@set_timeout(40, process_time)
def function():
	pass
	

超时信号只能设置在主线程函数,不能设置在子线程函数

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/350738.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号