Guest User

Untitled

a guest
Aug 16th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. import asyncio
  2. import random
  3. import traceback
  4.  
  5.  
  6. def something_that_happens_later(future: asyncio.Future):
  7. if random.randint(0, 1):
  8. future.set_result('Future is done')
  9. else:
  10. future.set_exception(ValueError('Future is fail'))
  11.  
  12.  
  13. async def main():
  14. my_future = asyncio.Future()
  15. asyncio.get_event_loop().call_later(2, something_that_happens_later, my_future)
  16. print('Waiting...')
  17. try:
  18. print(await my_future)
  19. except ValueError:
  20. traceback.print_exc()
  21.  
  22.  
  23. if __name__ == '__main__':
  24. asyncio.get_event_loop().run_until_complete(main())
Add Comment
Please, Sign In to add comment