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
2021-12-25 15:58:24 +08:00

68 lines
No EOL
1.4 KiB
Python

import datetime
import logging
import os
import subprocess
import traceback
from queue import Queue, Empty
from threading import Thread
from time import sleep
from init import init_bot
encode = 'UTF-8'
def enqueue_output(out, queue):
for line in iter(out.readline, b''):
queue.put(line)
out.close()
init_bot()
logging.basicConfig(format="%(msg)s", level=logging.INFO)
botdir = './core/bots/'
lst = os.listdir(botdir)
runlst = []
pidlst = []
for x in lst:
bot = os.path.abspath(f'{botdir}{x}/bot.py')
if os.path.exists(bot):
p = subprocess.Popen(f'python {bot}', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
cwd=os.path.abspath('.'))
runlst.append(p)
pidlst.append(p.pid)
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()
start = datetime.datetime.now()
try:
while True:
try:
line = q.get_nowait()
except Empty:
pass
else:
try:
logging.info(line[:-1].decode(encode))
except Exception:
print(line)
traceback.print_exc()
# break when all processes are done.
if all(p.poll() is not None for p in runlst):
break
sleep(0.001)
except KeyboardInterrupt:
for x in pidlst:
os.kill(x, 9)