From fec51e50f0f3d4c5bbe0bad6669204bc2df5f0d2 Mon Sep 17 00:00:00 2001 From: luyishisi <543429245@qq.com> Date: Mon, 16 Jan 2017 16:24:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E7=8E=B0=E8=BF=9B=E7=A8=8B=E8=B0=83?= =?UTF-8?q?=E7=94=A8phantomjs=E5=81=87=E6=AD=BB=E9=97=AE=E9=A2=98=E5=B7=B2?= =?UTF-8?q?=E7=BB=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 9.phantomjs/timeout/README.md | 2 ++ 9.phantomjs/timeout/timeout.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 9.phantomjs/timeout/README.md create mode 100644 9.phantomjs/timeout/timeout.py 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()