如何使用aiomultiprocess?
How to use aiomultiprocess?
我发现这个包 aiomultiprocess
似乎可以同时进行多处理和异步处理。
from aiohttp import request
from aiomultiprocess import Pool
async def get(url):
async with request("GET", url) as response:
return await response.text("utf-8")
async def main():
urls = ["https://jreese.sh", "https://www.google.com", ]
async with Pool() as pool:
async for result in pool.map(get, urls):
print(result)
尽管尝试 运行 示例代码,但完全没有任何作用。
尝试调用 main()
时出现错误 RuntimeWarning: coroutine 'main' was never awaited
。我找不到如何触发代码的实际示例。
关于此的唯一其他 question 没有回答。
aiomultiprocess
文档示例没有介绍如何调用循环。该函数需要通过asyncio调用。
import asyncio
from aiohttp import request
from aiomultiprocess import Pool
async def get(url):
async with request("GET", url) as response:
return await response.read()
async def main():
urls = ["https://jreese.sh", "https://www.google.com", ]
async with Pool() as pool:
async for result in pool.map(get, urls):
print(result)
if __name__ == '__main__':
# for Python 3.7
asyncio.run(main())
# for Python 3.6
# loop = asyncio.get_event_loop()
# loop.run_until_complete(main())
我发现这个包 aiomultiprocess
似乎可以同时进行多处理和异步处理。
from aiohttp import request
from aiomultiprocess import Pool
async def get(url):
async with request("GET", url) as response:
return await response.text("utf-8")
async def main():
urls = ["https://jreese.sh", "https://www.google.com", ]
async with Pool() as pool:
async for result in pool.map(get, urls):
print(result)
尽管尝试 运行 示例代码,但完全没有任何作用。
尝试调用 main()
时出现错误 RuntimeWarning: coroutine 'main' was never awaited
。我找不到如何触发代码的实际示例。
关于此的唯一其他 question 没有回答。
aiomultiprocess
文档示例没有介绍如何调用循环。该函数需要通过asyncio调用。
import asyncio
from aiohttp import request
from aiomultiprocess import Pool
async def get(url):
async with request("GET", url) as response:
return await response.read()
async def main():
urls = ["https://jreese.sh", "https://www.google.com", ]
async with Pool() as pool:
async for result in pool.map(get, urls):
print(result)
if __name__ == '__main__':
# for Python 3.7
asyncio.run(main())
# for Python 3.6
# loop = asyncio.get_event_loop()
# loop.run_until_complete(main())