Python从门到精通(六):线程-03-线程间通信

Python从门到精通(六):线程-03-线程间通信

原创

生而为人我很遗憾博主文章分类:python ©著作权

文章标签 优先级队列 协调者 线程池 python 文章分类 Python 后端开发

©著作权归作者所有:来自51CTO博客作者生而为人我很遗憾的原创作品,请联系作者获取转载授权,否则将追究法律责任

一、共享队列

1.1、​简单队列

from queue import Queuefrom threading import Threaddef producer(out_q):    while True:        data = 'hello world!'        out_q.put(data)def consumer(in_q):    while True:        data = in_q.get()        print(f'get data is: {data}')q = Queue()t1 = Thread(target=consumer, args=(q,))t2 = Thread(target=producer, args=(q,))t1.start()t2.start()

1.2、协调者队列

from queue import Queuefrom threading import Thread_sentinel = object()def producer(out_q):    put_time = 0    while True:        data = 'hello world!'        out_q.put(data)        put_time += 1        if put_time == 5:            out_q.put(_sentinel)def consumer(in_q):    while True:        data = in_q.get()        print(f'get data is: {data}')        if data is _sentinel:            in_q.put(_sentinel)            breakq = Queue()t1 = Thread(target=consumer, args=(q,))t2 = Thread(target=producer, args=(q,))t1.start()t2.start()

1.3、优先级队列

import heapqimport threadingclass PriorityQueue:    def __init__(self):        self._queue = []        self._count = 0        self._cv = threading.Condition()    def put(self, item, priority):        with self._cv:            heapq.heappush(self._queue, (-priority, self._count, item))            self._count += 1            self._cv.notify()    def get(self):        with self._cv:            while len(self._queue) == 0:                self._cv.wait()            return heapq.heappop(self._queue)[-1]

1.4、队列异常

import queueq = queue.Queue()try:    data = q.get(block=False)except queue.Empty:    ...try:    item = ''    q.put(item, block=False)except queue.Full:    ...try:    data = q.get(timeout=5.0)except queue.Empty:    ...

二、JOIN方法的使用

from queue import Queuefrom threading import Thread_sentinel = object()def producer(out_q):    put_time = 0    while True:        data = 'hello world!'        out_q.put(data)        put_time += 1        if put_time == 5:            out_q.put(_sentinel)def consumer(in_q):    while True:        data = in_q.get()        print(f'get data is: {data}')        if data is _sentinel:            in_q.put(_sentinel)            break        in_q.task_done()q = Queue()t1 = Thread(target=consumer, args=(q,))t2 = Thread(target=producer, args=(q,))t1.start()t2.start()q.join()

三、线程监听

from queue import Queuefrom threading import Thread, Eventdef producer(out_q):    while True:                ...                evt = Event()        data = ''        out_q.put((data, evt))        ...                evt.wait()def consumer(in_q):    while True:        data, evt = in_q.get()                ...                evt.set()

四、线程间复制

from queue import Queuefrom threading import Threadimport copydef producer(out_q):    while True:                ...        data = ''        out_q.put(copy.deepcopy(data))def consumer(in_q):    while True:                data = in_q.get()                ...

  • 收藏
  • 评论
  • *举报

上一篇:Python从门到精通(六):线程-02-线程池

下一篇:Python从门到精通(五):文件处理-06-ini文件处理

Original: https://blog.51cto.com/arch/5415519
Author: 生而为人我很遗憾
Title: Python从门到精通(六):线程-03-线程间通信

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/513850/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球