当前第2页 返回上一页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | from threading import Thread
from queue import Queue
class WorkerThread(Thread):
def __init__(self,*args,**kwargs):
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
if __name__== "__main__" :
w=WorkerThread()
w.start()
w.send( "php" )
w.send( "中" )
w.send( "文" )
w.send( "网" )
w.close()
|
运行结果:
相关推荐:
全面解析python线程优先级队列(queue)原理
Python中使用Queue和Condition进行线程同步的方法
以上就是Python线程下queue(队列)模块的用法(附实例)的详细内容,更多文章请关注木庄网络博客!!
返回前面的内容
相关阅读 >>
一种解释型语言--Python的介绍
Python中的切片是什么?(实例解析)
caffe导入到Python报错怎么办
Python按哪个键运行
详解Python使用asyncio包处理并发的方法
Python怎么读取图片
Python 统计字数的思路详解
Python中eval的用法详解及潜在风险介绍
【Python学习】面向对象的编程
Python安装哪个版本
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python线程下queue(队列)模块的用法(附实例)