前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python中子进程Timeout的使用

Python中子进程Timeout的使用

作者头像
py3study
发布2020-01-07 16:32:27
1.5K0
发布2020-01-07 16:32:27
举报
文章被收录于专栏:python3python3

   Python中并没有Timeout模块,但是在程序中经常遇到需要超时控制的情况。

   有两种思路去实现这种Timeout,其一是将需要做超时处理的代码块作为一个独立的子进程来处理,可将其另做一个包含main函数的.py文件。然后使用子进程超时限制来控制代码块运行的时间。

代码语言:javascript
复制
#!/usr/bin/python
import signal
import select
import datetime
import subprocess
import os,sys,time
'''input: command string split list'''
def timeoutCmd(command, timeout):
    start = datetime.datetime.now()
    process = subprocess.Popen(command,stdout=subprocess.PIPE)
    while process.poll() is None:
        time.sleep(0.2)
        now = datetime.datetime.now()
        if (now - start).seconds > timeout:
            try:
                os.kill(process.pid, signal.SIGKILL)
                os.waitpid(-1, os.WNOHANG)
            except:
                pass
            return False
    return True

  以上的方法可以实现将需要运行的命令作为command参数 ,成为当前进程的子进程,控制其运行时间。当子进程和主进程需要通信的时候,则可以使用文件通信。

代码语言:javascript
复制
'''input: command string'''
def timeoutCommand(command, timeout):
    outMsg = []
    pipeFile = "timeout_%s.txt" %time.mktime(time.localtime())
    fileWriter = open(pipeFile, "w")
    cmd = command.split()
    start = datetime.datetime.now()
    process = subprocess.Popen(cmd, universal_newlines=True, stdout=fileWriter)
    while process.poll() is None:
        time.sleep(0.2)
        now = datetime.datetime.now()
        if (now - start).seconds > timeout:
            try:
                os.kill(process.pid, signal.SIGKILL)
                os.waitpid(-1, os.WNOHANG)
            except:
                pass
            break
    try:
        fileWriter.close()
        fileReaser = open(pipeFile, "r")
        outMsg = fileReaser.readlines()
        fileReaser.close()
    except:
        pass
    try:
        os.remove(pipeFile)
    except:
        pass
    return outMsg

Python程序超时的情况经常出现,将核心而且容易超时的代码块封装成一个子进程来控制是一种十分稳妥的方法。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档