python thread+queue
# -*- coding: utf-8 -*-#
import threading
import time
from queue import Queue
def do(name):
print(name)
time.sleep(0.1)
def task(name):
if not message_queue.full():
message_queue.put(name)
else:
time.sleep(0.1)
def receive_message():
while True:
message = message_queue.get()
do(message)
message_queue.task_done()
def run():
for i in range(10):
task(i)
if __name__ == '__main__':
s_time = time.time()
message_queue = Queue(20)
for _ in range(5):
file_worker = threading.Thread(target=receive_message, daemon=True)
file_worker.start()
run()
message_queue.join()
print(f"耗时:{time.time() - s_time}")
不论你在什么时候开始,重要的是开始之后就不要停止。
不论你在什么时候结束,重要的是结束之后就不要悔恨。