scheduler.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import time
  2. from multiprocessing import Process
  3. from proxypool.api import app
  4. from proxypool.getter import Getter
  5. from proxypool.tester import Tester
  6. from proxypool.db import RedisClient
  7. from proxypool.setting import *
  8. class Scheduler():
  9. def schedule_tester(self, cycle=TESTER_CYCLE):
  10. """
  11. 定时测试代理
  12. """
  13. tester = Tester()
  14. while True:
  15. print('测试器开始运行')
  16. tester.run()
  17. time.sleep(cycle)
  18. def schedule_getter(self, cycle=GETTER_CYCLE):
  19. """
  20. 定时获取代理
  21. """
  22. getter = Getter()
  23. while True:
  24. print('开始抓取代理')
  25. getter.run()
  26. time.sleep(cycle)
  27. def schedule_api(self):
  28. """
  29. 开启API
  30. """
  31. app.run(API_HOST, API_PORT)
  32. def run(self):
  33. print('代理池开始运行')
  34. if TESTER_ENABLED:
  35. tester_process = Process(target=self.schedule_tester)
  36. tester_process.start()
  37. if GETTER_ENABLED:
  38. getter_process = Process(target=self.schedule_getter)
  39. getter_process.start()
  40. if API_ENABLED:
  41. api_process = Process(target=self.schedule_api)
  42. api_process.start()