Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python async function

import asyncio


async def eternity():
   # Sleep for 60 minutes
   await asyncio.sleep(3600)
   print('yay!')


async def main():
   try:
       # timeout if function takes longer than 1 second
       await asyncio.wait_for(eternity(), timeout=1.0)
   except asyncio.TimeoutError:
       print('timeout!')


asyncio.run(main())

# output timeout!

Source by codefreelance.net #
 
PREVIOUS NEXT
Tagged: #python #async #function
ADD COMMENT
Topic
Name
2+7 =