I need to make asynchronous requests using the Requests library. In Python 3.7 if I try from requests import async I get SyntaxError: invalid syntax.
async has become a reserved with in Python 3.7. How to I get around this situation?
I need to make asynchronous requests using the Requests library. In Python 3.7 if I try from requests import async I get SyntaxError: invalid syntax.
async has become a reserved with in Python 3.7. How to I get around this situation?
Lukasa who is with the requests lib said:
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
httplibto 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.
But don't worry aiohttp is very similar to requests.
Here's an example.
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())
You can use asyncio to make asynchronous requests. Here is an example:
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())
You could use hyper-requests(https://github.com/edjones84/hyper-requests), which allows you to pass in a list of url's and parameters to be run asynchronously, like so:
import hyper_requests
# Define the request parameters
params = [
{'url': 'http://httpbin.org/get' , 'data': 'value1'},
{'url': 'http://httpbin.org/get' , 'data': 'value3'},
{'url': 'http://httpbin.org/get' , 'data': 'value5'},
{'url': 'http://httpbin.org/get' , 'data': 'value7'},
{'url': 'http://httpbin.org/get' , 'data': 'value9'}
]
# Create an instance of AsyncRequests and execute the requests
returned_data = hyper_requests.get(request_params=params, workers=10)
# Process the returned data
for response in returned_data:
print(response)
requests library. The requests_async one is made by the same creator with the same API so they're different.
importlib.import_module(".async", "requests"). But actually I also getModuleNotFoundError: No module named 'requests.async'.requestslet go of theasyncmodule altogether a long time ago but the docs are not very clear about that.aiohttporgrequests.