python中startout是什么意思_Python中子进程Timeout的使用

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

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

#!/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参数 ,成为当前进程的子进程,控制其运行时间。当子进程和主进程需要通信的时候,则可以使用文件通信。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值