Advertisement
Guest User

Untitled

a guest
May 24th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. from time import time
  2. import asyncio
  3.  
  4. async def call_periodically(interval, func, *args, **kwargs):
  5.     start_time = time()
  6.     while True:
  7.         print('calling', func)
  8.         yield await func(*args, **kwargs)  # <-- I want this to block
  9.         await asyncio.sleep(
  10.             interval - ((time() - start_time) % interval)
  11.         )
  12.  
  13. async def sample_func():
  14.     print('sleeping...')
  15.     await asyncio.sleep(1)
  16.     print('sleep done')
  17.     return 'message'
  18.  
  19. async def get_messages(func, *args, **kwargs):
  20.     async for message in call_periodically(3, func, *args, **kwargs):
  21.         print(message)
  22.         #await self.message_handler(message)
  23.  
  24. loop = asyncio.get_event_loop()
  25. loop.run_until_complete(get_messages(sample_func))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement