简体   繁体   English

将带有对象的 function 传递到 concurrent.futures.ProcessPoolExecutor()?

[英]Pass function with objects into concurrent.futures.ProcessPoolExecutor()?

Need help passing objects into the cpu_bound function.需要帮助将对象传递到cpu_bound function。 The program uses both asyncio and multiprocessing , so if you know both, it would be the best kind of help!该程序同时使用asynciomultiprocessing ,因此如果您两者都知道,那将是最好的帮助!

Basically the problem arises at: result = loop.run_in_executor(pool, lambda: cpu_bound(list1, list2, int_var)基本上问题出现在: result = loop.run_in_executor(pool, lambda: cpu_bound(list1, list2, int_var)

I am not able to pass lambda function into the pool, and program errors with: _pickle.PicklingError: Can't pickle <function <lambda> at 0x00000230FDEDD700>: attribute lookup <lambda> on __main__ failed我无法将 lambda function 传递到池中,并且程序错误: _pickle.PicklingError: Can't pickle <function <lambda> at 0x00000230FDEDD700>: attribute lookup <lambda> on __main__ failed

Here is a mock structure of my program, since the whole program is over 2,000 lines of code:这是我的程序的模拟结构,因为整个程序有超过 2,000 行代码:

import ...
# Defining some functions...
.

def cpu_bound(list1, list2, int_var):
  # Some CPU-bound calculations...
  .

async def find_trades(session, list3, list4):
  # Some async function calls
  .
  with concurrent.futures.ProcessPoolExecutor() as pool:
    result = loop.run_in_executor(
        pool, dill.loads(dill.dumps(lambda: cpu_bound(list1, list2, int_var)))
    try:
        await asyncio.wait_for(
            result, timeout=5
        )
    except asyncio.TimeoutError:
        print("Took to long to compute!")

async def run():
  # Some async function calls
  .
  await asyncio.gather(find_trades(session, list3, list4), ...)

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())
    loop.close()

Unfortunately, I am relatively new to multiprocessing and might not know a lot of things about restrictions that come with passing objects from the main program's loop into multi-processed parts of it.不幸的是,我对多处理相对较新,并且可能不知道很多关于将对象从主程序循环传递到它的多处理部分所带来的限制。

Really appreciate all the help!真的很感谢所有的帮助!

The following line:以下行:

    loop.run_in_executor(
        pool, dill.loads(dill.dumps(lambda: cpu_bound(list1, list2, int_var)))

doesn't seem to make much sense.似乎没有多大意义。 You are serializing a lambda using dill , but then you're immediately deserializing it back into a lambda before it has a chance to be transferred to the subprocess.您正在使用dill序列化 lambda ,但随后您会立即将其反序列化回 lambda ,然后才有机会转移到子进程。 This is probably why you are getting an error from pickle, despite attempting to use dill.这可能就是为什么你会从 pickle 中得到一个错误,尽管你尝试使用 dill。

But you can avoid a lambda in the first place by passing the intended arguments as positional arguments to run_in_executor :但是您可以首先通过将预期的 arguments 作为位置 arguments 传递给 run_in_executor 来run_in_executor

result = loop.run_in_executor(pool, cpu_bound, list1, list2, int_var)

If the lists contain picklable objects, this should work just fine.如果列表包含可腌制对象,这应该可以正常工作。

暂无
暂无

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

相关问题 如何将 Popen 对象传递给 concurrent.futures.ProcessPoolExecutor - How to pass Popen objects to concurrent.futures.ProcessPoolExecutor 如何在 Python 中的 concurrent.futures.ProcessPoolExecutor 中传递“锁”? - How to pass the “lock” in my concurrent.futures.ProcessPoolExecutor in Python? 如何将多个参数传递给由 concurrent.futures.ProcessPoolExecutor 中的 executor.map() 迭代的函数 - How to pass several parameters to a function which is iterated by executor.map() from concurrent.futures.ProcessPoolExecutor 当 function 是 lambda 或嵌套的 function 时,concurrent.futures.ProcessPoolExecutor 挂起 - concurrent.futures.ProcessPoolExecutor hangs when the function is a lambda or nested function concurrent.futures.ProcessPoolExecutor() 中的共享变量 python - Shared variable in concurrent.futures.ProcessPoolExecutor() python 使用初始化启动 concurrent.futures.ProcessPoolExecutor? - Launch concurrent.futures.ProcessPoolExecutor with initialization? 为什么 concurrent.futures.ProcessPoolExecutor() 跳过迭代? - Why is concurrent.futures.ProcessPoolExecutor() skipping iterations? 如何使用 concurrent.futures.ProcessPoolExecutor 在进程之间传递消息/信息 - How to pass messages/information between process using concurrent.futures.ProcessPoolExecutor 为 concurrent.futures.ProcessPoolExecutor 播种 numpy.random 的 default_rng 和 SeedSequence 对象 - Seeding numpy.random's default_rng and SeedSequence objects for concurrent.futures.ProcessPoolExecutor Python 上的 `concurrent.futures.ProcessPoolExecutor` 从文件开头而不是定义的函数运行 - `concurrent.futures.ProcessPoolExecutor` on Python is ran from beginning of file instead of the defined function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM