当前位置:首页 > python > 正文内容

Python eventlet 模块,Timeout() 控制子程序运行时间

root6年前 (2020-11-16)python3204

pip install  eventlet #安装依赖包

# -*- coding:utf-8 -*-
import eventlet
import time

eventlet.monkey_patch()  # 这行代码必须加

def test():
    print("test 运行开始")
    time.sleep(5)
    print("test 运行结束")

with eventlet.Timeout(4, False):
    print("开始")
    test()
print("时间到了")
import eventlet
import time

eventlet.monkey_patch()  # 这行代码必须加

def test():
    print("test 运行开始")
    time.sleep(5)
    print("test 运行结束")

try:
    with eventlet.Timeout(1, True):
        print("开始")
        test()
except eventlet.timeout.Timeout:
    print ("捕捉到了")
print("时间到了")


扫描二维码推送至手机访问。

版权声明:本文由一叶知秋发布,如需转载请注明出处。

本文链接:https://www.zhiqiu.top/?id=38

分享给朋友:

相关文章

自定义logger 模块使调用打印日志的文件为调用文件而不是logger模块

更新logging 源码1248行f f f.f_back替换为f f f.f_back     f_2 f.f_back   ...

python 的configparser 读取配置文件遇到%特殊符号

test.ini 配置文件中有mysql的密码,且密码含有“%”这个特殊符号因为%在py是转义符的含义需要对该字符转义即修改  %  为 %%用%对%进行转义...

cmd启动python交互模式 出现UnicodeDecodeError: 'gbk' codec can't decode byte 0x9a in position 533

这是因为在python交互模式的中输出了中文,且是个输出被记录在.python_history中删除历史记录文件C:\Users\Administrator\.python_history...

python用requests发送模拟请求忽略https的认证,忽略警告

import warnings warnings.filterwarnings('ignore')在文件头添加忽略警告信息的输出r = requests.get('https://kyfw.12306.cn&#...

python 运行 出现 BrokenPipeError: [Errno 32] Broken pipe

这里是因为window创建多线程的问题window下创建线程有限制,当然linux下创建的线程也是有限制的但是window的数据是比较低的fromhttps://blog.csdn.net/qq_33666011/article/detai...

python 调用linux命令 subprocess.popen

import subprocesscommd = "echo 123"p1 = subprocess.Popen(commd, shell=True, stdout=subprocess.PIPE, stder...