python 进程超时控制 防止phantomjs假死
- 2019 年 11 月 28 日
- 筆記
目前的程序结构是一个主进程控制50个线程进行数据采集,采集的请求方式使用开进程调用phantomjs去发出带浏览器处理能力的请求。
环境
linux python 2.7
phantomjs
问题
phantomjs运作中卡死,导致调用其的线程长时间等待。
解决方案
用下代码将启动phantomjs的进程用做超时设置
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 stdout print time.ctime() finally: print time.ctime() my_timer.cancel()
原创文章,转载请注明: 转载自URl-team