Answer the question
In order to leave comments, you need to log in
ES7 -> python side by side?
Hello. You need to quickly master python, or rather its abilities in asynchrony. I worked with JS ES7, including async / await, so I'll ask: is there a side by side comparison of code in JS ES7 and python? Thus, understanding the language will be much easier.
Thanks in advance.
Answer the question
In order to leave comments, you need to log in
https://www.google.com.ua/search?q=python+for+js+devs
https://dev.to/underdogio/python-for-javascript-de...
https://learnxinyminutes.com /docs/python3/
async/await has been around since version 3.5. There is a separate aiohttp for http.
https://asyncio.readthedocs.io/en/latest/
https://aiohttp.readthedocs.io/en/stable/
Looks like this
ASYNCIO:
import asyncio
async def say(what, when):
await asyncio.sleep(when)
print(what)
async def stop_after(loop, when):
await asyncio.sleep(when)
loop.stop()
loop = asyncio.get_event_loop()
loop.create_task(say('first hello', 2))
loop.create_task(say('second hello', 1))
loop.create_task(say('third hello', 4))
loop.create_task(stop_after(loop, 3))
loop.run_forever()
loop.close()
import aiohttp
import asyncio
import async_timeout
async def fetch(session, url):
with async_timeout.timeout(10):
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)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
from aiohttp import web
async def handle(request):
name = request.match_info.get('name', "Anonymous")
text = "Hello, " + name
return web.Response(text=text)
app = web.Application()
app.router.add_get('/', handle)
app.router.add_get('/{name}', handle)
web.run_app(app)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question