Advertisement
Guest User

Untitled

a guest
Feb 6th, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. import asyncio
  2.  
  3. SCHEDULE_PERIOD = 60*60
  4. USERS = []
  5.  
  6.  
  7. async def fetch_from_makaka():
  8.     result = None
  9.     await asyncio.sleep(15)  # emulate some work
  10.     return result
  11.  
  12.  
  13. async def telega_send(user, data):
  14.     await asyncio.sleep(2)  # emulate some work
  15.    
  16.  
  17. async def notify_users(data):
  18.     coros = [telega_send(u, data) for u in USERS]
  19.     _ = await asyncio.gather(*coros)
  20.  
  21.  
  22. async def periodic():
  23.     while True:
  24.         print('Start periodic makaka fetch')
  25.         result = await fetch_from_makaka()
  26.         await notify_users(result)
  27.         await asyncio.sleep(SCHEDULE_PERIOD)
  28.  
  29.  
  30. def stop():
  31.     task.cancel()
  32.  
  33.  
  34. if __name__ == "__main__":
  35.     task = asyncio.Task(periodic())
  36.     loop = asyncio.get_event_loop()
  37.     loop.call_later(5, stop)
  38.     try:
  39.         loop.run_until_complete(task)
  40.     except asyncio.CancelledError:
  41.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement