start_mail_alert.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. '''Created 16 Nov 2022 Levi'''
  2. from collections import deque
  3. from threading import Event
  4. from threading import Thread
  5. from time import sleep
  6. import asyncio
  7. import mail_alert
  8. # event1 = Event()
  9. # event2 = Event()
  10. # event3 = Event()
  11. # thread1 = Thread(target=mail_alert.mail_alert, args=(event1,))
  12. # thread2 = Thread(target=mail_alert.mail_alert2, args=(event2,))
  13. # thread3 = Thread(target=mail_alert.mail_alert3, args=(event3,))
  14. # thread4 =
  15. # thread5 =
  16. # thread6 =
  17. async def main() -> None:
  18. container: set = set()
  19. try:
  20. while True:
  21. # loop = asyncio.get_event_loop()
  22. # tasks = map(asyncio.create_task,
  23. # [mail_alert.mail_alert(container)
  24. # # mail_alert.mail_alert2()
  25. # # mail_alert.mail_alert3()
  26. # ])
  27. tasks = [mail_alert.mail_alert(container),
  28. mail_alert.mail_alert2(container),
  29. mail_alert.mail_alert3(container)]
  30. # result = await asyncio.wait(tasks)
  31. results = await asyncio.gather(*tasks)
  32. # print(result)
  33. for result in results:
  34. if result not in container:
  35. container.add(result)
  36. # await asyncio.sleep(1)
  37. except KeyboardInterrupt:
  38. print('Cancelling all tasks...')
  39. for task in tasks:
  40. task.cancel() # noqa
  41. await asyncio.gather(*tasks, return_exceptions=True)
  42. print('All tasks cancelled.')
  43. if __name__ == '__main__':
  44. asyncio.run(main())