python 进程超时控制 防止phantomjs假死
目前的程序结构是一个主进程控制50个线程进行数据采集,采集的请求方式使用开进程调用phantomjs去发出带浏览器处理能力的请求。
环境
linux python 2.7
phantomjs
问题
phantomjs运作中卡死,导致调用其的线程长时间等待。
解决方案
用下代码将启动phantomjs的进程用做超时设置
Python
importsubprocessfromthreadingimportTimerimporttimekill=lambdaprocess: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 stdoutprinttime.ctime()finally:printtime.ctime()my_timer.cancel() |
本文暂时没有评论,来添加一个吧(●'◡'●)