[英]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.