Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import time
- import asyncio
- async def call_periodically(interval, func, *args, **kwargs):
- start_time = time()
- while True:
- print('calling', func)
- yield await func(*args, **kwargs) # <-- I want this to block
- await asyncio.sleep(
- interval - ((time() - start_time) % interval)
- )
- async def sample_func():
- print('sleeping...')
- await asyncio.sleep(1)
- print('sleep done')
- return 'message'
- async def get_messages(func, *args, **kwargs):
- async for message in call_periodically(3, func, *args, **kwargs):
- print(message)
- #await self.message_handler(message)
- loop = asyncio.get_event_loop()
- loop.run_until_complete(get_messages(sample_func))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement