博客
关于我
python 多进程-进阶-进程间通信之Queue
阅读量:795 次
发布时间:2023-03-07

本文共 2183 字,大约阅读时间需要 7 分钟。

Python多进程通信:Queue实现多进程间数据传递

在多进程编程中,进程间通信是实现程序运行的重要环节。Python提供了多种进程间通信的方式,其中Queue(队列)是一种多进程安全的队列,适用于多个进程之间进行数据传递。

Queue的基本使用

Queue支持两种操作:put(插入数据)和get(读取数据)。这两种操作都具有两个可选参数:blockedtimeout

Put方法

put方法用于将数据插入队列中。该方法的两个可选参数如下:

  • blocked(默认值为True):如果设置为True且timeout为正值,方法会阻塞指定的时间,直到队列有空余空间。如果在等待时间内没有空间可用,则会抛出Queue.Full异常。
  • timeout(默认值为None):如果设置为正值,方法会在指定时间后抛出Queue.Empty异常。如果blocked为False,且队列已满,则会立即抛出Queue.Full异常。

Get方法

get方法用于从队列中读取并删除一个元素。该方法的两个可选参数如下:

  • blocked(默认值为True):如果设置为True且timeout为正值,方法会阻塞指定的时间,等待队列中有数据可读。如果在等待时间内没有数据可读,则会抛出Queue.Empty异常。
  • timeout(默认值为None):如果设置为正值,方法会在指定时间后抛出Queue.Empty异常。如果blocked为False,则会在没有数据时立即抛出Queue.Empty异常。

示例:多进程间的数据传递

以下是一个典型示例,展示了如何在多个进程之间使用Queue进行数据传递:

# -*- coding: utf-8 -*-from multiprocessing import Process, Queueimport os, time, randomdef proce_write(q, urls):    print("Process (%s) is writing..." % os.getpid())    for url in urls:        q.put(url)        print("Put %s to queue..." % url)        time.sleep(random.random())def proc_read(q):    print('Process (%s) is reading...' % os.getpid())    while True:        url = q.get(True)        print('Get %s from queue.' % url)if __name__ == "__main__":    q = Queue()    proc_writer1 = Process(target=proce_write, args=(q, ['url_1', 'url_2', 'url_3']))    proc_writer2 = Process(target=proce_write, args=(q, ['url_4', 'url_5', 'url_6']))    proc_reader = Process(target=proc_read, args=(q,))    proc_writer1.start()    proc_writer2.start()    proc_reader.start()    proc_writer1.join()    proc_writer2.join()    proc_reader.terminate()

运行输出

运行上述代码可以观察到以下输出:

Process (12204) is writing...Put url_1 to queue...Process (12724) is writing...Put url_4 to queue...Process (5484) is reading...Get url_1 from queue.Get url_4 from queue.Process (12204) is writing...Put url_2 to queue...Get url_2 from queue.Process (12724) is writing...Put url_5 to queue...Get url_5 from queue.Process (12204) is writing...Put url_3 to queue...Get url_3 from queue.Process (12724) is writing...Put url_6 to queue...Get url_6 from queue.Process finished with exit code 0

总结

通过上述示例可以看出,Queue是一种高效的进程间通信工具,能够在多个进程之间安全地传递数据。其putget方法提供了灵活的阻塞和超时控制选项,适用于不同的应用场景。在实际应用中,可以根据具体需求选择合适的blockedtimeout参数,确保进程间的通信更加高效和可靠。

转载地址:http://biofk.baihongyu.com/

你可能感兴趣的文章
python | xonsh,一个超酷的 Python 库!
查看>>
python | yagmail,一个实用的 Python 库!
查看>>
python | 一文掌握Python的上下文管理器和with语句
查看>>
python | 一文看懂Python闭包机制与变量作用域规则
查看>>
python读取含中文的json
查看>>
python | 如何用Python锁避免并发错误?
查看>>
python | 提升代码迭代速度的Python重载方法
查看>>
python | 深入理解Python并发编程中的GIL限制与解决方案
查看>>
Python | 爬虫实战——亚马逊搜索页监控(附详细源码)
查看>>
python | 高效使用Python工具自动生成模块文档的秘诀
查看>>
python 一个list去除另一个list中的值
查看>>
python 三大框架的 介绍。
查看>>
Python 下载的 11 种姿势,一种比一种高级!
查看>>
python读取一个文件夹下所有图片_初学Python-找出文件夹下的所有图片
查看>>
Python 中 3 个不可思议的返回功能
查看>>
python 中 dict 的另一种用法
查看>>
Python 中 PIL 读取图片出现异常旋转的解决方法
查看>>
python读取word表格内容(1)
查看>>
python 中os.path.join 双斜杠的解决办法
查看>>
python 中PIL.Image和OpenCV图像格式相互转换
查看>>