2008年9月10日水曜日

code snipet for child process with timeout.

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク
子プロセス。loop.py

import time
import sys

time.sleep(2)
print 1000


親プロセス。selectで3秒まつ。何も書き出されなかったらtimeout。
書き出されたらそれをprintして終了。

import select
import subprocess

p = subprocess.Popen(['/usr/bin/python','./loop.py'], stdout=subprocess.PIPE)

i, o, e =select.select([p.stdout],[], [], 3)
assert not o
assert not e

if i:
print 'done! with ', i[0].read()
else:
print 'time out'

0 件のコメント: