当前第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和matlab的区别
Python中不等于如何表示
Python代码段有哪些
搭建 Python +pycharm+django将sqlite3 迁移到mysql
vscode下好用的Python插件及配置_Python
Python中flask应用(表单处理)
如何利用Python实现图片转字符画详解
迅速掌握Python中的hook钩子函数
Python代码是什么语言
Python爬取豆瓣电影数据并且提取值xpath和lxml模块(代码)
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python线程下queue(队列)模块的用法(附实例)