I'd like to change the processing from several requests modules to aiohttp code.

Asked 1 years ago, Updated 1 years ago, 95 views

 position_count = []
    for matchid in matchid_list:
        url = "https://api.***.com/matches/" + matchid + "?apikey=***"
        temp_dict = requests.get(url).json()
        try:
            for i in range(0, 10):
                if user == temp_dict['players'][i]['nickname']:
                    count = temp_dict['players'][i]['position']['name']
                    position_count.append(count)
        except IndexError:
            pass
    position_count2 = Counter(position_count).most_common()

It is a code that retrieves about 100 json files and extracts the desired value from the dict form, and I learned about aiohttp module because it can be processed faster if it is asynchronously.

   position_count = []
    async def get_position(matchid: str):
        async with aiohttp.request('GET', 'https://api.***.com/matches/'+matchid+'?apikey=***') as resp:
            print(resp.json)

    tasks = [get_position(matchid) for matchid in matchid_list]    
    asyncio.run(asyncio.wait(tasks))

    position_count2 = Counter(position_count).most_common()

When rep.json is running, you can see that you are sending the correct url. However, I have a question because there is a blockage in the part where each json is put in a variable called temp_json in a dict form and indexed. Thank you.

asynchronous aiohttp

2022-09-21 15:24

1 Answers

Doing it asynchronously would mean that the order is not important.

If you want to see an asynchronous effect without changing the questioner's code as much as possible, change the get_position function as follows.

You can get all the results of the counter and continue the post-processing with _c.

_c = []
async get_position (matchid: str, c: List): # c is the container to store Counter (position_count)
    ...
    ...

    c.extends(Counter(position_count).most_common())

...
...
tasks = [get_position(matchid, _c) for matchid in matchid_list] 


2022-09-21 15:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.