diff --git a/9.phantomjs/timeout/README.md b/9.phantomjs/timeout/README.md new file mode 100644 index 00000000..b6fbd111 --- /dev/null +++ b/9.phantomjs/timeout/README.md @@ -0,0 +1,2 @@ +在程序调用phantomjs的时候,可能因phantomjs死锁导致的进程僵死。。 +测试进程下设置超时时间。 diff --git a/9.phantomjs/timeout/timeout.py b/9.phantomjs/timeout/timeout.py new file mode 100644 index 00000000..4d73b90a --- /dev/null +++ b/9.phantomjs/timeout/timeout.py @@ -0,0 +1,19 @@ +import subprocess +from threading import Timer +import time + +kill = lambda process: process.kill() + +cmd = ["ping", "www.google.com"] +ping = subprocess.Popen( + cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + +my_timer = Timer(5, kill, [ping]) +try: + my_timer.start() + stdout, stderr = ping.communicate() + #print stderr + print time.ctime() +finally: + print time.ctime() + my_timer.cancel()