python - 当多个子进程使用 `queue.Queue`进行访问时, `concurrent.futures.ProcessPoolExecutor`是线程安全的吗?

标签 python python-3.x multithreading

我经常在执行多个线程的情况下广泛使用queue.Queue。通过使用concurrent.futures.ThreadPoolExecutor

我从博客中了解到queue.Queue应该是线程安全的,但这是否意味着它是线程安全的,前提是Python解释器一次只能执行一个线程( GIL ),或者它也是线程安全的。在使用multiprocessing的情况下,通过使用子进程而不是线程来回避 GIL

https://docs.python.org/3/library/concurrent.futures.html#processpoolexecutor

最佳答案

ProcessPoolExecutorcall queue使用multiprocessing.queues.Queue,对result queue使用mp_context.SimpleQueue(multiprocessing)-用于在本地线程和进程之间进行通信。
Nice graphic of ProcessPoolExecutor

  • current.futures.ProcessPoolExecutor东西使用多处理队列在线程和进程之间进行通信。
  • multiprocessing.queues.Queue文档特别声明它是线程进程安全
  • queue documentation的底部,有一条注释指向multiprocessing.Queue对象... for use in a multi-processing (rather than multi-threading) context
  • 关于python - 当多个子进程使用 `queue.Queue`进行访问时, `concurrent.futures.ProcessPoolExecutor`是线程安全的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61328662/

    相关文章:

    python - 对 Python 字典列表进行分组

    python - pygame图像碰撞问题

    python - 我在哪里输入我的代码(Pyside2)?

    python-3.x - Boost Python 示例失败两次

    python - 使用 Python 线程对具有速率限制的慢速 API 进行数千次调用

    c++ - 包装原子类型并确保它保持原子性

    c++ - 在 C++ 中将指针转换为 volatile void**

    python - Keras TimeDistributed 层实际上是做什么的?

    python - Django 管理 - 添加自定义链接以更改 ListView

    python - 沿 N 维 numpy 数组的每个维度对每个第 n 个元素进行索引