execute a command with timeout in python


class Command(threading.Thread):
def __init__(self, command, cwd=None, writeConsole=True, timeout=60):
threading.Thread.__init__(self)
self.command = command
self.cwd = cwd
self.writeConsole = writeConsole
self.timeout = timeout


def run(self):
if self.cwd is not None:
print '[' + ' '.join(self.command) + '] in ' + self.cwd
else:
print '[' + ' '.join(self.command) + ']'
self.p = subprocess.Popen(self.command, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, cwd=self.cwd)

self.stdout_lines = []
for stdout_line in iter(self.p.stdout.readline, b''):
if self.writeConsole:
sys.stdout.write(stdout_line)
self.stdout_lines.append(stdout_line)

self.stderr_lines = []
for stderr_line in iter(self.p.stderr.readline, b''):
if self.writeConsole:
sys.stderr.write(stderr_line)
self.stderr_lines.append(stderr_line)

self.result = self.p.poll()


def exec_command(self):
self.start()
self.join(self.timeout)

if self.is_alive():
print 'executing [' + ' '.join(self.command) + '] timeout with timeout in seconds =>', self.timeout
self.p.terminate() #use self.p.kill() if process needs a kill -9
self.join()
sys.exit(1)
return (self.result, self.stdout_lines, self.stderr_lines)


if __name__ == "__main__":
result, outs, errs = Command(
["/bin/sleep", "3"], writeConsole=True, timeout=2).exec_command()
Command(
["/bin/echo", "3"], writeConsole=True, timeout=2).exec_command()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值