简体   繁体   English

如果从函数内部执行,带有“ apply_async”的多处理池不执行任何操作

[英]Multiprocessing pool with “apply_async” does nothing if executed from inside a function

I'm trying to use the multiprocessing module and more partuclarly the Pool.apply_async() function. 我正在尝试使用multiprocessing模块,尤其是使用Pool.apply_async()函数。

This code works well: 该代码运行良好:

import multiprocessing

def do():
    print("Foobar", flush=True)

with multiprocessing.Pool(1) as pool:
    for i in range(2):
        pool.apply_async(do)

    pool.close()
    pool.join()

The "Foobar" string is printed twice. "Foobar"字符串被打印两次。

However, if I put this code in a function and then call this function, nothing happens. 但是,如果我将此代码放入一个函数中,然后调用该函数,则不会发生任何事情。 No error nor "Foobar" , the program ends silently. 没有错误也没有"Foobar" ,程序将以静默方式结束。

import multiprocessing

def test():

    def do():
        print("Foobar", flush=True)

    with multiprocessing.Pool(1) as pool:
        for i in range(5):
            pool.apply_async(do)

        pool.close()
        pool.join()

test()

Why that? 为什么? I'm using Python 3.7.3 on Linux. 我在Linux上使用Python 3.7.3。

In order to retrieve your computation results do the following change to your code. 为了检索您的计算结果,请对代码进行以下更改。

import multiprocessing

def test():

    def do():
        print("Foobar", flush=True)

    with multiprocessing.Pool(1) as pool:
        for i in range(5):
            result = pool.apply_async(do)

            result.get()

        pool.close()
        pool.join()

test()

You will see the reason why "nothing happens". 您将看到“什么都没有发生”的原因。

Traceback (most recent call last):
  File "/tmp/test.py", line 17, in <module>
    test()
  File "/tmp/test.py", line 12, in test
    result.get()
  File "/usr/lib/python3.5/multiprocessing/pool.py", line 608, in get
    raise self._value
  File "/usr/lib/python3.5/multiprocessing/pool.py", line 385, in _handle_tasks
    put(task)
  File "/usr/lib/python3.5/multiprocessing/connection.py", line 206, in send
    self._send_bytes(ForkingPickler.dumps(obj))
  File "/usr/lib/python3.5/multiprocessing/reduction.py", line 50, in dumps
    cls(buf, protocol).dump(obj)
AttributeError: Can't pickle local object 'test.<locals>.do'    

Python multiprocessing.Pool relies on the pickle protocol to serialize the data to be sent to the other process. Python multiprocessing.Pool依赖于pickle协议来序列化要发送到其他进程的数据。 The pickle protocol can serialize only top level functions and not nested ones. pickle协议只能序列化顶级功能,而不能嵌套功能。

To see what can be pickled and what cannot check the documentation . 要查看可以腌制的内容和不能查看文档的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多处理池apply_async - Multiprocessing pool apply_async 为什么我的 multiprocessing.Pool apply_async 只在 for 循环中执行一次 - Why is my multiprocessing.Pool apply_async only executed once inside a for loop 多处理池&#39;apply_async&#39;似乎只调用一次函数 - Multiprocessing pool 'apply_async' only seems to call function once 如何使用python多处理池apply_async函数在自己的类中分配回调 - How to assign callback inside own class using python multiprocessing pool apply_async function Python多处理池apply_async错误 - Python multiprocessing pool apply_async error Python 中的异步多处理与池 apply_async - Asynchronous multiprocessing in Python with pool apply_async Python多处理:apply_async()中的print() - Python multiprocessing: print() inside apply_async() multiprocessing.Pool:何时使用 apply、apply_async 或 map? - multiprocessing.Pool: When to use apply, apply_async or map? 如何使用 multiprocessing.Pool 判断 apply_async 函数是否已启动或仍在队列中 - How to tell if an apply_async function has started or if it's still in the queue with multiprocessing.Pool 如何将multiprocessing.Pool实例传递给apply_async回调函数? - How to pass multiprocessing.Pool instance to apply_async callback function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM