Python搭建代理池,为你的爬虫程序保驾护航( 二 )
class Scheduler(object):"""Scheduler"""def run_tester(self, cycle=CYCLE_TESTER):"""定时启动检测器:param cycle::return:"""if not ENABLE_TESTER:app_logger.info('tester not enabled, exit')returntester = Tester()sch.add_job(tester.run, 'interval', seconds=cycle, id='run_tester')sch.start()def run_getter(self, cycle=CYCLE_GETTER):"""定时开启爬虫补充代理:param cycle::return:"""if not ENABLE_GETTER:app_logger.info('getter not enabled, exit')returngetter = Getproxies()sch.add_job(getter.run, 'interval', seconds=cycle, id='run_getter')sch.start()def run_server(self):"""web服务端开启"""if not ENABLE_SERVER:app_logger.info('server not enabled, exit')returnapp.run(host=API_HOST, port=API_PORT, threaded=API_THREADED)def run(self):"""调度器启动"""global tester_process, getter_process, server_processtry:app_logger.info('starting proxypool...')if ENABLE_TESTER:tester_process = multiprocessing.Process(target=self.run_tester)app_logger.info(f'starting tester, pid {tester_process.pid}...')tester_process.start()if ENABLE_GETTER:getter_process = multiprocessing.Process(target=self.run_getter)# print(dir(getter_process),getter_process)app_logger.info(f'starting getter, pid{getter_process.pid}...')getter_process.start()if ENABLE_SERVER:server_process = multiprocessing.Process(target=self.run_server)app_logger.info(f'starting server, pid{server_process.pid}...')server_process.start()tester_process.join()getter_process.join()server_process.join()except KeyboardInterrupt:app_logger.info('received keyboard interrupt signal')tester_process.terminate()getter_process.terminate()server_process.terminate()finally:# must call join method before calling is_alivetester_process.join()getter_process.join()server_process.join()app_logger.info(f'tester is {"alive" if tester_process.is_alive() else "dead"}')app_logger.info(f'getter is {"alive" if getter_process.is_alive() else "dead"}')app_logger.info(f'server is {"alive" if server_process.is_alive() else "dead"}')app_logger.info('proxy terminated')
- 票票|淘票票经营主体公司改名,经营范围新增文艺经纪代理
- 告诉|阿里大佬告诉你如何一分钟利用Python在家告别会员看电影
- Python源码阅读-基础1
- Python调用时使用*和**
- 如何基于Python实现自动化控制鼠标和键盘操作
- 解决多版本的python冲突问题
- 学习python第二弹
- Python中文速查表-Pandas 基础
- 零基础小白Python入门必看:通俗易懂,搞定深浅拷贝
- Python 使用摄像头监测心率!这么强吗?