[英]starting event loop in subprocess
我正在使用aiomultiprocess
来并行化我的程序。
代码:
import asyncio
from aiomultiprocess import Pool
async def c(index):
print("c" + index)
def main(index):
print("b")
asyncio.run(c(index))
if __name__ == "__main__":
print("a")
with Pool() as pool:
pool.map(main, [i for i in range(2)])
错误:
Traceback (most recent call last):
File "C:\------------------------------------------------\main.py", line 16, in <module>
with Pool() as pool:
AttributeError: __enter__
我曾尝试使用multiprocessing
而不是aiomultiprocess
但错误更糟(更正确的时间更长)。 谁能帮我?
这应该可以正常工作:
import asyncio
from aiomultiprocess import Pool
async def c(index):
print("c" + str(index))
async def main(index):
print("b")
async with Pool() as pool:
pool.map(c, [i for i in range(2)])
if __name__ == "__main__":
asyncio.run(main())
我认为您的问题源于尝试使用普通with
而不是async with
创建Pool()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.