main.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import asyncio
  2. import os.path
  3. from concurrent.futures import ThreadPoolExecutor
  4. from arty.thread_queue import ThreadQueue
  5. from arty.worker import Worker
  6. from arty.renderer import Renderer
  7. from arty.utils import get_token, run_in_thread
  8. from arty.bot import Bot
  9. def _init():
  10. lock = os.path.join(os.path.dirname(__file__), '.first_init')
  11. if os.path.isfile(lock):
  12. return
  13. print('running first time setup. this will take some time.')
  14. renderer = Renderer()
  15. renderer.make_image('something cute', num_images=1)
  16. with open(lock, 'w') as f:
  17. f.write('done')
  18. print('first time setup done.')
  19. async def _main():
  20. worker_queue = ThreadQueue()
  21. bot_queue = ThreadQueue()
  22. worker = Worker(bot_queue, worker_queue)
  23. bot = Bot(get_token('discord'), bot_queue, worker_queue)
  24. executor = ThreadPoolExecutor(max_workers=2)
  25. loop = asyncio.get_event_loop()
  26. await asyncio.wait(
  27. fs={
  28. loop.run_in_executor(executor, run_in_thread, worker),
  29. loop.run_in_executor(executor, run_in_thread, bot),
  30. },
  31. return_when=asyncio.ALL_COMPLETED
  32. )
  33. if __name__ == '__main__':
  34. _init()
  35. asyncio.run(_main())