发布时间:2019-09-12 07:58:26编辑:auto阅读(1791)
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)
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实现Restful API
47848
46402
37290
34738
29321
25977
24922
19955
19549
18032
5796°
6420°
5936°
5965°
7071°
5919°
5950°
6444°
6407°
7786°