import asyncio
async defprint_B(): #Simple async defprint("B")
async defmain_def():
print("A")
await asyncio.gather(print_B())
print("C")
asyncio.run(main_def())
# The function you wait for must include async# The function you use await must include async# The function you use await must run by asyncio.run(THE_FUNC())
Posted by: Guest
on February-04-2021
0
python async await
import signal
import sys
import asyncio
import aiohttp
import json
loop = asyncio.get_event_loop()
client = aiohttp.ClientSession(loop=loop)
async defget_json(client, url):
async with client.get(url) as response:
assert response.status ==200return await response.read()
async defget_reddit_top(subreddit, client):
data1 = await get_json(client, 'https://www.reddit.com/r/'+ subreddit +'/top.json?sort=top&t=day&limit=5')
j = json.loads(data1.decode('utf-8'))
for i in j['data']['children']:
score = i['data']['score']
title = i['data']['title']
link = i['data']['url']
print(str(score) +': '+ title +' ('+ link +')')
print('DONE:', subreddit +'n')
defsignal_handler(signal, frame):
loop.stop()
client.close()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
asyncio.ensure_future(get_reddit_top('python', client))
asyncio.ensure_future(get_reddit_top('programming', client))
asyncio.ensure_future(get_reddit_top('compsci', client))
loop.run_forever()
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems
resetting your password contact us
Check Your Email and Click on the link sent to your email