fenghzy 发表于 2018-8-7 11:43:19

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程序超时的情况经常出现,将核心而且容易超时的代码块封装成一个子进程来控制是一种十分稳妥的方法。
页: [1]
查看完整版本: Python中子进程Timeout的使用