Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. import asyncio
  2.  
  3. SCHEDULE_PERIOD = 60*60*6
  4. EMAILS = []
  5.  
  6.  
  7. async def fetch_from_db():
  8.     result_file = None
  9.     await asyncio.sleep(15)  # emulate some work
  10.     return result_file
  11.  
  12.  
  13. async def create_pdf(input_file):
  14.     pdf_file = None
  15.     await asyncio.sleep(15)  # emulate some work
  16.     return pdf_file
  17.  
  18.  
  19. async def send_to(email, pdf_file):
  20.     await asyncio.sleep(2)  # emulate some work
  21.    
  22.  
  23. async def email_users(pdf_file):
  24.     coros = [send_to(e, pdf_file) for e in EMAILS]
  25.     _ = await asyncio.gather(*coros)
  26.  
  27.  
  28. async def periodic():
  29.     while True:
  30.         print('Start periodic fetch from db')
  31.         result_file = await fetch_from_db()
  32.         pdf_file = await create_pdf(result_file)
  33.         await email_users(pdf_file)
  34.         await asyncio.sleep(SCHEDULE_PERIOD)
  35.  
  36.  
  37. def stop():
  38.     task.cancel()
  39.  
  40.  
  41. if __name__ == "__main__":
  42.     task = asyncio.Task(periodic())
  43.     loop = asyncio.get_event_loop()
  44.     loop.call_later(5, stop)
  45.     try:
  46.         loop.run_until_complete(task)
  47.     except asyncio.CancelledError:
  48.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement