Skip to content

Instantly share code, notes, and snippets.

@thanakijwanavit
Created March 21, 2021 15:30
Show Gist options
  • Save thanakijwanavit/394830a2395330b0a07db1fd4f69cc46 to your computer and use it in GitHub Desktop.
Save thanakijwanavit/394830a2395330b0a07db1fd4f69cc46 to your computer and use it in GitHub Desktop.
async await example in python
import requests, time, random
import asyncio
from asgiref.sync import sync_to_async
from datetime import datetime
async def post(url, body):
t0 = datetime.now()
r = await sync_to_async(requests.post)(url,json=body)
return r.json()
async def main():
url = 'https://hkf9vqpqe4.execute-api.ap-southeast-1.amazonaws.com/Prod/generate'
body = {"branchId" : "test"}
r = await asyncio.gather(*(post(url, body) for i in range(100)))
return r
await main() # in jupyter notebook
if __name__ == "__main__": # using command line execution
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment