thread_pool.py 652 B

1234567891011121314151617181920212223242526272829
  1. import logging
  2. import time
  3. from threading import Thread
  4. def process():
  5. while True:
  6. try:
  7. if len(queue) > 0:
  8. fn = queue.pop()
  9. fn()
  10. except Exception as e:
  11. logging.error(e)
  12. finally:
  13. time.sleep(1)
  14. # 使用 gunicorn 时, gunicorn 会 fork 出若干个子进程, 每个进程都有自己的 worker_thread, 且内存不共享
  15. worker_thread = None
  16. queue = []
  17. def submit(job, callback):
  18. global worker_thread
  19. if worker_thread is None:
  20. worker_thread = Thread(target=process)
  21. worker_thread.start()
  22. queue.append(lambda: job(callback))