3. Synchronous vs Asynchronous
• Synchronous Execution
• My boss is a busy man. He tells me to write the code. I tell him: Fine. I get
started and he's watching me like a vulture, standing behind me, off my
shoulder. I'm like "Dude, WTF: why don't you go and do something while I
finish this?"
• he's like: "No, I'm waiting right here until you finish." This is synchronous.
• Asynchronous Execution
• The boss tells me to do it, and rather than waiting right there for my work,
the boss goes off and does other tasks. When I finish my job I simply report
to my boss and say: "I'm DONE!" This is Asynchronous Execution.
Source:stackoverflow
4. Lets take a real world example to justify the difference
• Django can perform all kind of CRUD operations but can perform a single
request one after one
• Lets take a real world example if I want to manipulate a data transmitted by a
user and perform a logic before adding it to database
def post(request): def get(request,data):
return data
if request.method==Post:
data=get(data)
if data is not None:
data.save()
8. Python asynchronous frameworks
• Tornado
• Twisted
• Asyncio(aiohttp for server/client)
• And we will use aiohttp framework to make a part of my function
asynchronous
10. import asyncio
import time
async def say_after(delay, what):
await asyncio.sleep(delay)
print(what)
async def main():
print(f"started at {time.strftime('%X')}")
await say_after(1, 'hello')
await say_after(2, 'world')
print(f"finished at {time.strftime('%X')}")
asyncio.run(main())
started at 17:13:52
hello
world
finished at 17:13:55
11. async def main():
task1 = asyncio.create_task(
say_after(1, 'hello'))
task2 = asyncio.create_task(
say_after(2, 'world'))
print(f"started at {time.strftime('%X')}")
# Wait until both tasks are completed (should take
# around 2 seconds.)
await task1
await task2
print(f"finished at {time.strftime('%X')}")
started at 17:14:32
hello
world
finished at 17:14:34
12. Now lets have a greater understanding on how all this things works and how are going to
use the response generated from Django and perform our logic and then submitting this to
database
By creating a asynchronous functions using AIOHTTP
We will just create a simple otp function when ever a user signsup the user will be asked to
verify his otp so that his otp gets verified and he will be added to the database
13. Basic knowledge on aiohttp
async with aiohttp.ClientSession() as session:
async with session.get('http://httpbin.org/get') as resp:
print(resp.status)
print(await resp.text())
• session.put('http://httpbin.org/put', data=b'data') session.delete('http://httpbin.org/delete’)
• session.head('http://httpbin.org/get’)
• session.options('http://httpbin.org/get’)
• session.patch('http://httpbin.org/patch', data=b'data')
14.
15. async def index(request):
async with aiohttp.ClientSession() as client:
data=await(email_sending(client))
print(data)
await
client.post('http://127.0.0.1:8000/acc/signup/',data=data)
return web.Response(text="OK")
async with aiohttp.ClientSession() a
16. async def email_sending(client):
async with client.get('http://www.mocky.io/v2/5c23aef12f00003300049691') as resp:
resp=await(resp.json())
totp = pyotp.TOTP('base32secret3232')
otp=totp.now()
smtp.sendmail("you@gmail.com",resp['email'], otp)
sucess=asyncio.create_task(email_verification(otp))
return resp