当前位置:Gxlcms > Python > Python实现线程池代码分享

Python实现线程池代码分享

时间:2021-07-01 10:21:17 帮助过:54人阅读

原理:建立一个任务队列,然多个线程都从这个任务队列中取出任务然后执行,当然任务队列要加锁,详细请看代码

  1. import threading
  2. import time
  3. import signal
  4. import os
  5. class task_info(object):
  6. def __init__(self):
  7. self.func = None
  8. self.parm0 = None
  9. self.parm1 = None
  10. self.parm2 = None
  11. class task_list(object):
  12. def __init__(self):
  13. self.tl = []
  14. self.mutex = threading.Lock()
  15. self.sem = threading.Semaphore(0)
  16. def append(self, ti):
  17. self.mutex.acquire()
  18. self.tl.append(ti)
  19. self.mutex.release()
  20. self.sem.release()
  21. def fetch(self):
  22. self.sem.acquire()
  23. self.mutex.acquire()
  24. ti = self.tl.pop(0)
  25. self.mutex.release()
  26. return ti
  27. class thrd(threading.Thread):
  28. def __init__(self, tl):
  29. threading.Thread.__init__(self)
  30. self.tl = tl
  31. def run(self):
  32. while True:
  33. tsk = self.tl.fetch()
  34. tsk.func(tsk.parm0, tsk.parm1, tsk.parm2)
  35. class thrd_pool(object):
  36. def __init__(self, thd_count, tl):
  37. self.thds = []
  38. for i in range(thd_count):
  39. self.thds.append(thrd(tl))
  40. def run(self):
  41. for thd in self.thds:
  42. thd.start()
  43. def func(parm0=None, parm1=None, parm2=None):
  44. print 'count:%s, thrd_name:%s'%(str(parm0), threading.currentThread().getName())
  45. def cleanup(signo, stkframe):
  46. print ('Oops! Got signal %s', signo)
  47. os._exit(0)
  48. if __name__ == '__main__':
  49. signal.signal(signal.SIGINT, cleanup)
  50. signal.signal(signal.SIGQUIT, cleanup)
  51. signal.signal(signal.SIGTERM, cleanup)
  52. tl = task_list()
  53. tp = thrd_pool(6, tl)
  54. tp.run()
  55. count = 0
  56. while True:
  57. ti = task_info()
  58. ti.parm0 = count
  59. ti.func = func
  60. tl.append(ti)
  61. count += 1
  62. time.sleep(2)
  63. pass

人气教程排行