python线程池超过最大数量_python自定义线程池控制线程数量
1.自定義線程池import threading
import Queue
import time
queue = Queue.Queue()
def put_data_in_queue():
for i in xrange(10):
queue.put(i)
class MyThread(threading.Thread):
def run(self):
while not queue.empty():
sleep_times = queue.get()
time.sleep(sleep_times)
queue.task_done()
def main_function():
threads_num = 6
while True:
put_data_in_queue()
for i in xrange(threads_num):
myThread = MyThread()
myThread.setDaemon(True)
myThread.start()
queue.join()
time.sleep(60)
2.多線程與signal信號的監控結合import threading
import Queue
import time
import signal
queue = Queue.Queue()
stop = False
def receive_signal(signum, stack):
signal.signal(signal.SIGTERM, original_sigterm)
global stop
stop = True
def put_data_in_queue():
for i in xrange(10):
queue.put(i)
class MyThread(threading.Thread):
def run(self):
while not queue.empty():
sleep_times = queue.get()
time.sleep(sleep_times)
queue.task_done()
def main_function():
threads_num = 6
while not stop:
put_data_in_queue()
for i in xrange(threads_num):
myThread = MyThread()
myThread.setDaemon(True)
myThread.start()
queue.join()
time.sleep(60)
if __name__ == "__main__":
original_sigterm = signal.getsignal(signal.SIGTERM)
signal.signal(signal.SIGTERM, receive_signal)
main_function()
總結
以上是生活随笔為你收集整理的python线程池超过最大数量_python自定义线程池控制线程数量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python supervisor_py
- 下一篇: 小程序onload_小程序生命周期-基础