队列-单线程
import threading
import queue#不是线程模块的队列
import time
class worker(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
self.thread_stop = False
def run(self):
while not self.thread_stop:
print(f"{self.name} id:{self.ident} 等待测试")
try:
task = q.get(block=True, timeout=2) # 接收消息
except queue.Empty:
print("工作结束! 我可以回家了!")
self.thread_stop = True
break
print(f"\t工作任务:{task[0]} 任务编号:{task[1]}")
print("\t工作中...")
time.sleep(1.2)
print("\t工作结束!")
q.task_done() # 完成一个任务
res = q.qsize() # 判断消息队列大小(队列中还有几个任务)
if res > 0:
print(f"\t还有{res}个任务要做,加油!")
def stop(self):
self.thread_stop = True
if __name__ == "__main__":
q = queue.Queue(3) # 创建队列(大小为3)
worker = worker(q) # 将队列加入类中
print("***********************领导:开始工作!")
worker.start() # 启动类
q.put(["produce cup!", 1], block=True, timeout=None) # 向队列中添加元素,产生任务消息
q.put(["produce desk!", 2], block=True, timeout=None)
q.put(["produce apple!", 3], block=True, timeout=None)
q.put(["produce banana!", 4], block=True, timeout=None)
q.put(["produce bag!", 5], block=True, timeout=None)
q.join() # 等待所有任务完成
print("***********************领导:所有任务完成!")
输出:
***********************领导:开始工作!
Thread-1 id:10344 等待测试
工作任务:produce cup! 任务编号:1
工作中...
工作结束!
还有3个任务要做,加油!
Thread-1 id:10344 等待测试
工作任务:produce desk! 任务编号:2
工作中...
工作结束!
还有3个任务要做,加油!
Thread-1 id:10344 等待测试
工作任务:produce apple! 任务编号:3
工作中...
工作结束!
还有2个任务要做,加油!
Thread-1 id:10344 等待测试
工作任务:produce banana! 任务编号:4
工作中...
工作结束!
还有1个任务要做,加油!
Thread-1 id:10344 等待测试
工作任务:produce bag! 任务编号:5
工作中...
工作结束!
Thread-1 id:10344 等待测试
***********************领导:所有任务完成!
工作结束! 我可以回家了!
[Finished in 8.1s]
队列-多线程
import threading
import time
import logging
from queue import Queue
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [*] %(message)s"
)
img_lists = ['lipei_00006.mp3', 'lipei_00007.mp3', 'lipei_00012.mp3', 'lipei_00014.mp3',
'lipei_00021.mp3', 'lipei_00027.mp3', 'lipei_00028.mp3', 'lipei_00035.mp3',
'lipei_00039.mp3', 'lipei_00044.mp3', 'lipei_00047.mp3', 'lipei_00049.mp3',
'lipei_00057.mp3', 'lipei_00058.mp3', 'lipei_00059.mp3', 'lipei_00061.mp3',
'lipei_00066.mp3', 'lipei_00068.mp3', 'lipei_00070.mp3', 'lipei_00081.mp3',
'lipei_00087.mp3', 'lipei_00104.mp3', 'lipei_00106.mp3', 'lipei_00117.mp3',
'lipei_00123.mp3', 'lipei_00129.mp3', ]
class Music_Cols(threading.Thread):
def __init__(self, name, queue):
super().__init__(name=name)
self.queue = queue
def run(self):
global img_lists
while True:
try:
music = img_lists.pop(0)
self.queue.put(music)
except IndexError:
break
class Music_Play(threading.Thread):
def __init__(self, name, queue):
super().__init__(name=name)
self.queue = queue
def run(self):
while True:
if self.queue.not_empty:
music = q.get()
logging.info(f"{threading.current_thread()}正在播放{music}")
time.sleep(2)
q.task_done()
logging.info(f'{music}播放结束')
else:
break
if __name__ == '__main__':
q = Queue(10)
mc_thread = Music_Cols('music_cols', q)
mc_thread.setDaemon(True) # 设置为守护进程,主线程退出时,子进程也kill掉
mc_thread.start() # 启动进程
for _ in range(5): # 设置线程个数(批量任务时,线程数不必太大,注意内存及CPU负载)
mp_thread = Music_Play('music_play', q)
mp_thread.setDaemon(True)
mp_thread.start()
q.join() # 线程阻塞(等待所有子线程处理完成,再退出)
输出:
2019-12-08 16:10:58,391 [*] <Music_Play(music_play, started daemon 12460)>正在播放lipei_00006.mp3
2019-12-08 16:10:58,407 [*] <Music_Play(music_play, started daemon 9084)>正在播放lipei_00007.mp3
2019-12-08 16:10:58,409 [*] <Music_Play(music_play, started daemon 11296)>正在播放lipei_00012.mp3
2019-12-08 16:10:58,410 [*] <Music_Play(music_play, started daemon 12048)>正在播放lipei_00014.mp3
2019-12-08 16:10:58,410 [*] <Music_Play(music_play, started daemon 8208)>正在播放lipei_00021.mp3
2019-12-08 16:11:00,411 [*] lipei_00021.mp3播放结束
2019-12-08 16:11:00,411 [*] lipei_00014.mp3播放结束
2019-12-08 16:11:00,411 [*] lipei_00012.mp3播放结束
2019-12-08 16:11:00,412 [*] <Music_Play(music_play, started daemon 11296)>正在播放lipei_00035.mp3
2019-12-08 16:11:00,411 [*] lipei_00006.mp3播放结束
2019-12-08 16:11:00,412 [*] <Music_Play(music_play, started daemon 12460)>正在播放lipei_00039.mp3
2019-12-08 16:11:00,411 [*] <Music_Play(music_play, started daemon 8208)>正在播放lipei_00027.mp3
2019-12-08 16:11:00,411 [*] lipei_00007.mp3播放结束
2019-12-08 16:11:00,412 [*] <Music_Play(music_play, started daemon 9084)>正在播放lipei_00044.mp3
2019-12-08 16:11:00,411 [*] <Music_Play(music_play, started daemon 12048)>正在播放lipei_00028.mp3
2019-12-08 16:11:02,415 [*] lipei_00028.mp3播放结束
2019-12-08 16:11:02,415 [*] lipei_00044.mp3播放结束
2019-12-08 16:11:02,415 [*] lipei_00027.mp3播放结束
2019-12-08 16:11:02,415 [*] lipei_00039.mp3播放结束
2019-12-08 16:11:02,415 [*] lipei_00035.mp3播放结束
2019-12-08 16:11:02,415 [*] <Music_Play(music_play, started daemon 12048)>正在播放lipei_00047.mp3
2019-12-08 16:11:02,415 [*] <Music_Play(music_play, started daemon 9084)>正在播放lipei_00049.mp3
2019-12-08 16:11:02,416 [*] <Music_Play(music_play, started daemon 8208)>正在播放lipei_00057.mp3
2019-12-08 16:11:02,416 [*] <Music_Play(music_play, started daemon 12460)>正在播放lipei_00058.mp3
2019-12-08 16:11:02,416 [*] <Music_Play(music_play, started daemon 11296)>正在播放lipei_00059.mp3
2019-12-08 16:11:04,418 [*] lipei_00059.mp3播放结束
2019-12-08 16:11:04,418 [*] lipei_00058.mp3播放结束
2019-12-08 16:11:04,418 [*] lipei_00057.mp3播放结束
2019-12-08 16:11:04,419 [*] lipei_00049.mp3播放结束
2019-12-08 16:11:04,419 [*] lipei_00047.mp3播放结束
2019-12-08 16:11:04,419 [*] <Music_Play(music_play, started daemon 11296)>正在播放lipei_00061.mp3
2019-12-08 16:11:04,419 [*] <Music_Play(music_play, started daemon 12460)>正在播放lipei_00066.mp3
2019-12-08 16:11:04,419 [*] <Music_Play(music_play, started daemon 8208)>正在播放lipei_00068.mp3
2019-12-08 16:11:04,420 [*] <Music_Play(music_play, started daemon 9084)>正在播放lipei_00070.mp3
2019-12-08 16:11:04,420 [*] <Music_Play(music_play, started daemon 12048)>正在播放lipei_00081.mp3
2019-12-08 16:11:06,423 [*] lipei_00068.mp3播放结束
2019-12-08 16:11:06,423 [*] <Music_Play(music_play, started daemon 8208)>正在播放lipei_00087.mp3
2019-12-08 16:11:06,423 [*] lipei_00081.mp3播放结束
2019-12-08 16:11:06,423 [*] lipei_00066.mp3播放结束
2019-12-08 16:11:06,423 [*] lipei_00061.mp3播放结束
2019-12-08 16:11:06,423 [*] lipei_00070.mp3播放结束
2019-12-08 16:11:06,423 [*] <Music_Play(music_play, started daemon 12048)>正在播放lipei_00104.mp3
2019-12-08 16:11:06,423 [*] <Music_Play(music_play, started daemon 12460)>正在播放lipei_00106.mp3
2019-12-08 16:11:06,423 [*] <Music_Play(music_play, started daemon 11296)>正在播放lipei_00117.mp3
2019-12-08 16:11:06,423 [*] <Music_Play(music_play, started daemon 9084)>正在播放lipei_00123.mp3
2019-12-08 16:11:08,426 [*] lipei_00117.mp3播放结束
2019-12-08 16:11:08,426 [*] <Music_Play(music_play, started daemon 11296)>正在播放lipei_00129.mp3
2019-12-08 16:11:08,426 [*] lipei_00123.mp3播放结束
2019-12-08 16:11:08,426 [*] lipei_00106.mp3播放结束
2019-12-08 16:11:08,426 [*] lipei_00104.mp3播放结束
2019-12-08 16:11:08,426 [*] lipei_00087.mp3播放结束
2019-12-08 16:11:10,428 [*] lipei_00129.mp3播放结束
[Finished in 12.2s]