Python线程下queue(队列)模块的用法(附实例)


当前第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()

运行结果:

1

2

3

4

php

相关推荐:

全面解析python线程优先级队列(queue)原理

Python中使用Queue和Condition进行线程同步的方法

以上就是Python线程下queue(队列)模块的用法(附实例)的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

Python if else用法是什么?

Python能做什么项目

Python解释器的提示符是什么

Python线程下条件变量的用法

史上最高效的Python爬虫框架(推荐)

解决Python大批量读写.doc文件的问题

如何用Python计算基本统计值?

Python数据分析用什么数据库

Python在实际工作中的应用有哪些

Python代码的单行注释使用什么符号

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...