Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/bot.py

58 lines
1.3 KiB
Python
Raw Normal View History

2021-08-07 12:55:07 +00:00
import logging
2021-02-01 15:25:57 +00:00
import os
2021-08-07 12:55:07 +00:00
import subprocess
2021-08-23 12:44:31 +00:00
import traceback
2021-08-07 12:55:07 +00:00
from queue import Queue, Empty
from threading import Thread
2021-08-21 07:45:21 +00:00
2021-08-23 12:44:31 +00:00
from init import init_bot
2021-08-21 07:45:21 +00:00
encode = 'UTF-8'
2020-09-19 10:35:13 +00:00
2021-08-21 15:58:07 +00:00
2021-08-07 12:55:07 +00:00
def enqueue_output(out, queue):
for line in iter(out.readline, b''):
queue.put(line)
out.close()
2021-02-01 15:13:11 +00:00
2021-08-21 15:58:07 +00:00
2021-08-20 16:32:46 +00:00
init_bot()
2021-03-05 16:19:06 +00:00
2021-08-20 16:32:46 +00:00
logging.basicConfig(format="%(msg)s", level=logging.INFO)
botdir = './core/bots/'
lst = os.listdir(botdir)
runlst = []
for x in lst:
2021-08-23 15:20:42 +00:00
bot = os.path.abspath(f'{botdir}{x}/bot.py')
2021-08-20 16:32:46 +00:00
if os.path.exists(bot):
2021-08-23 15:28:05 +00:00
p = subprocess.Popen(f'python {bot}', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
2021-08-21 15:58:07 +00:00
cwd=os.path.abspath('.'))
2021-08-20 16:32:46 +00:00
runlst.append(p)
q = Queue()
threads = []
for p in runlst:
threads.append(Thread(target=enqueue_output, args=(p.stdout, q)))
for t in threads:
t.daemon = True
t.start()
while True:
try:
line = q.get_nowait()
except Empty:
pass
except KeyboardInterrupt:
for x in runlst:
x.kill()
else:
2021-08-07 12:55:07 +00:00
try:
2021-08-21 07:45:21 +00:00
logging.info(line[:-1].decode(encode))
2021-08-20 16:32:46 +00:00
except Exception:
2021-08-21 07:45:21 +00:00
print(line)
2021-08-20 16:32:46 +00:00
traceback.print_exc()
2021-08-07 12:55:07 +00:00
2021-08-20 16:32:46 +00:00
# break when all processes are done.
if all(p.poll() is not None for p in runlst):
break