I am trying to parallelize requests to the Wikidata API using Python’s asyncio module.
My current synchronous script does the following:
JavaScript
x
14
14
1
import requests
2
3
base_url = "https://www.wikidata.org/w/api.php&"
4
payload = {
5
"action": "query",
6
"list": "search",
7
"srsearch": search_term,
8
"language": "en",
9
"format": "json",
10
"origin": "*",
11
}
12
res = requests.get(base_url, params=payload)
13
14
I am trying to do the same using asyncio
, to send requests asynchronously.
From this blogpost and the documentation, I understood that I need something like:
JavaScript
1
6
1
from aiohttp import ClientSession
2
3
async with ClientSession() as session:
4
async with session.get(url) as response:
5
response = await response.read()
6
However, I did not manage to find how to add these payloads in the request. Do I have to reconstruct the URL manually or is there a way to send the payloads in asyncio?
Advertisement
Answer
Try the following code
JavaScript
1
4
1
async with ClientSession() as session:
2
async with session.get(url, params=payload) as response:
3
response = await response.read()
4