Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- SCHEDULE_PERIOD = 60*60*6
- EMAILS = []
- async def fetch_from_db():
- result_file = None
- await asyncio.sleep(15) # emulate some work
- return result_file
- async def create_pdf(input_file):
- pdf_file = None
- await asyncio.sleep(15) # emulate some work
- return pdf_file
- async def send_to(email, pdf_file):
- await asyncio.sleep(2) # emulate some work
- async def email_users(pdf_file):
- coros = [send_to(e, pdf_file) for e in EMAILS]
- _ = await asyncio.gather(*coros)
- async def periodic():
- while True:
- print('Start periodic fetch from db')
- result_file = await fetch_from_db()
- pdf_file = await create_pdf(result_file)
- await email_users(pdf_file)
- await asyncio.sleep(SCHEDULE_PERIOD)
- def stop():
- task.cancel()
- if __name__ == "__main__":
- task = asyncio.Task(periodic())
- loop = asyncio.get_event_loop()
- loop.call_later(5, stop)
- try:
- loop.run_until_complete(task)
- except asyncio.CancelledError:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement