在Python3.7中使用请求库进行异步请求

2024-05-15 08:11:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要使用请求库制作asynchronous requests。在Python 3.7中,如果我尝试from requests import async,就会得到SyntaxError: invalid syntax

async已经变成了reserved with in Python 3.7。我怎样才能避开这种情况?


Tags: infromimportasyncwith情况requestsasynchronous
2条回答

您可以使用asyncio发出异步请求。下面是一个例子:

import asyncio
import requests

async def main():
    loop = asyncio.get_event_loop()
    futures = [
        loop.run_in_executor(
            None, 
            requests.get, 
            'http://example.org/'
        )
        for i in range(20)
    ]
    for response in await asyncio.gather(*futures):
        pass

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

与lib一起请求的Lukasa说:

At the current time there are no plans to support async and await. This is not because they aren't a good idea: they are. It's because to use them requires quite substantial code changes. Right now requests is a purely synchronous library that, at the bottom of its stack, uses httplib to send and receive data. We cannot move to an async model unless we replace httplib. The best we could do is provide a shorthand to run a request in a thread, but asyncio already has just such a shorthand, so I don't believe it would be valuable. Right now I am quietly looking at whether we can rewrite requests to work just as well in a synchronous environment as in an async one. However, the reality is that doing so will be a lot of work, involving rewriting a lot of our stack, and may not happen for many years, if ever.

但别担心aiohttp与请求非常相似。

这里有一个例子。

import aiohttp
import asyncio

async def fetch(session, url):
    async with session.get(url) as response:
        return await response.text()

async def main():
    async with aiohttp.ClientSession() as session:
        html = await fetch(session, 'http://python.org')
        print(html)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

相关问题 更多 >