1 Star 0 Fork 1

pish7/stdpython

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
queue_01.py 984 Bytes
一键复制 编辑 原始数据 按行查看 历史
pish7 提交于 2023-10-05 21:56 +08:00 . python
import threading
from queue import Queue
#工作线程
class WorkerThread(threading.Thread):
def __init__(self,*args,**kwargs):
threading.Thread.__init__(self,*args,**kwargs)
self.input_queue = Queue() #消息队列
def send(self,item): #发送消息
self.input_queue.put(item)
def close(self): #结束线程
self.input_queue.put(None)
self.input_queue.join()
def run(self): #线程函数
while True:
item = self.input_queue.get()
if item is None: #哨兵
break
# 处理项(使用有用的工作代替)
print(item)
self.input_queue.task_done() #项已处理完成
# 完成。指示收到和返回了哨兵
self.input_queue.task_done()
return
# 使用示例
w = WorkerThread()
w.start()
w.send("hello") # 将项发送给工作线程(通过队列)
w.send("world")
w.close()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/pish7/stdpython.git
git@gitee.com:pish7/stdpython.git
pish7
stdpython
stdpython
master

搜索帮助