[英]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!该程序同时使用asyncio和multiprocessing ,因此如果您两者都知道,那将是最好的帮助!
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.