'session methods aren't working in python async request
I am trying to make an async request using the session object.
What I have done so far is:
Setting a clientSession obj:
async with aiohttp.ClientSession() as session:Pass the session to method:
full_process(user, email, session)Call methods for api calls:
async def full_process(user, email, session):
res1 = await get_data(user, session)
res2 = await get_image(user, session)
- Perform api calls:
async def get_data(user, session):
r = await session.get('SOME_URL')
return r
The problem here is that, in step 4, the line for the await process doesn't even execute, or that's what I think. I simply added two print functions to verify the problem:
async def get_data(user, session):
print('before call')
r = await session.get('SOME_URL')
print('RESULT: ', r)
return r
but the second print is never executed
What am I doing wrong? Sorry if it is something obvious I am new to python Im using python 3.6.7
Here's the full code:
async def get_data(user, session):
print('EXEC DATA')
r1 = await session.get('SOME_URL')
print('R1: ' + str(r1))
return r1
async def get_image(user, session):
print('EXEC IMAGE')
r2 = await session.get('SOME_URL')
print('R2: ' + str(r2))
return r2
async def full_process(user, email, session):
print('FULL PROCESS STARTED')
res1 = await get_data(user, session)
print('FIRST RES: ' + str(res1))
if res1 and 'email' in res1:
res2 = await get_image(user, session)
print('SEC RES: ' + str(res2))
async def main_block():
tasks = []
some_data = [{some_obj}, {some_obj}, {some_obj}]
async with aiohttp.ClientSession() as session:
for line in some_data:
tasks.append(full_process(user, email, session))
await asyncio.gather(*tasks, return_exceptions=True)
print('Starting...')
loop = asyncio.get_event_loop()
loop.run_until_complete(main_block())
print('End...')
Executing this code gives me this result (simulating 3 items in some_data array):
Starting...
FULL PROCESS STARTED
EXEC DATA
FULL PROCESS STARTED
EXEC DATA
FULL PROCESS STARTED
EXEC DATA
End...
I just want to point out that the full script works. There are no errors, it starts and finish without issues, but obviously the api calls aren't made.
Thanks in advance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
