Python subprocess.Popen一次又一次地打断了我的大脑

2024-09-29 23:32:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我的任务是在parralel中为我的电报机器人和用户机器人创建一个启动器,并让它们的日志在同一个终端中。因此,我的逻辑是:创建4个线程:一个用于bot,第二个用于userbot,最后两个用于函数,这些函数应该对前两个进程进行分析,并在bot死亡时执行一些操作。这是我的代码:

import datetime
import os
import subprocess
import time


def botwaiter(process_object):
    bot_alive = True
    while bot_alive:
        time.sleep(1)
        check = process_object.poll()
        if check == None:
            pass
        else:
            bot_alive = False
    print(f'[BotWaiter]-[{datetime.datetime.now()}] Main bot process has been terminated, checking files...')
    if os.path.exists('stopping'):
        print(f'[BotWaiter]-[{datetime.datetime.now()}] Bot has stopped due to stop request, doing nothing...')
        os.remove('stopping')
        return
    elif os.path.exists('updating'):
        print(f'[BotWaiter]-[{datetime.datetime.now()}] Bot has stopped due to update request, updating...')
        update_process = subprocess.run("git pull", shell=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        if update_process.stderr is not None:
            update_process = subprocess.run("git stash && git pull", shell=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            print(f'[BotWaiter]-[{datetime.datetime.now()}] Update complete, starting back...')
            os.remove('updating')
            main('bot')
        else:
            print(f'[BotWaiter]-[{datetime.datetime.now()}] Update complete, starting back...')
            os.remove('updating')
            main('bot')
    elif os.path.exists('updatingdb'):
        print(f'[BotWaiter]-[{datetime.datetime.now()}] Bot has stopped due to update database request, updating...')
        update_process = subprocess.run("git commit main.dm -m \"Updated DB from Termux.\"", shell=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        if update_process.stderr is not None:
            print('Updating DB ended with error, cannot continue!\nError code:' + update_process.stderr)
            os.remove('updatingdb')
            return
        else:
            os.remove('updatingdb')
            main('bot')
    else:
        print(f'[BotWaiter]-[{datetime.datetime.now()}] Bot has stopped due to unknown reason, restarting...')
        time.sleep(1)
        main('bot')

def userbotwaiter(process_object):
    bot_alive = True
    while bot_alive:
        time.sleep(1)
        check = process_object.poll()
        if check == None:
            pass
        else:
            bot_alive = False
    print(f'[UserbotWaiter]-[{datetime.datetime.now()}] Main userbot process has been terminated, checking files...')
    if os.path.exists('ubstop'):
        print(f'[UserbotWaiter]-[{datetime.datetime.now()}] Userbot has stopped due to stop request, doing nothing...')
        os.remove('ubstop')
        return
    else:
        print(f'[UserbotWaiter]-[{datetime.datetime.now()}] Userbot has stopped due to unknown reason, restarting...')
        time.sleep(1)
        main('userbot')

def main(object_to_run = 'all'):
    try:
        if object_to_run == 'bot':
            print(f'[MainThread]-[{datetime.datetime.now()}] Starting bot...')
            bot_process = subprocess.Popen('python3 bot.py', shell=True)
            subprocess.Popen(botwaiter(bot_process))
            print(f'[MainThread]-[{datetime.datetime.now()}] Started bot!')
        elif object_to_run == 'userbot':
            print(f'[MainThread]-[{datetime.datetime.now()}] Starting userbot...')
            userbot_process = subprocess.Popen('python3 userbot.py', shell=True)
            subprocess.Popen(userbotwaiter(userbot_process))
            print(f'[MainThread]-[{datetime.datetime.now()}] Started userbot!')
        else:
            print(f'[MainThread]-[{datetime.datetime.now()}] Starting bot...')
            bot_process = subprocess.Popen('python3 bot.py', shell=True)
            subprocess.Popen(botwaiter(bot_process))
            print(f'[MainThread]-[{datetime.datetime.now()}] Started bot!')
            print(f'[MainThread]-[{datetime.datetime.now()}] Starting userbot...')
            userbot_process = subprocess.Popen('python3 userbot.py', shell=True)
            subprocess.Popen(userbotwaiter(userbot_process))
            print(f'[MainThread]-[{datetime.datetime.now()}] Started userbot!')
    except KeyboardInterrupt:
        print(f'[BotWaiter]-[{datetime.datetime.now()}] Catched Ctrl+C, stopping...')
        try:
            bot_process.kill()
            print(f'[BotWaiter]-[{datetime.datetime.now()}] Bot has been killed successfully')
        except:
            print(f'[BotWaiter]-[{datetime.datetime.now()}] Error while killing bot, please use pkill python3')
        try:
            userbot_process.kill()
            print(f'[BotWaiter]-[{datetime.datetime.now()}] Userbot has been killed successfully')
        except:
            print(f'[BotWaiter]-[{datetime.datetime.now()}] Error while killing userbot, please use pkill python3')
        exit()
os.system('clear')
main()

而且,它打破了我所有的逻辑!它似乎冻结在第81行或第82行(因为userbot处于脱机状态,甚至没有83的行输出!)(第81行是main()func,else语句,行以“bot\u process=“))开头,但bot和botwaier工作正常。。。直到服务员需要做些工作。它只是在完成作业后将此错误抛给我:

Traceback (most recent call last):
  File "run.py", line 102, in <module>
    main()
  File "run.py", line 82, in main
    subprocess.Popen(botwaiter(bot_process))
  File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.8/subprocess.py", line 1569, in _execute_child
    args = list(args)
TypeError: 'NoneType' object is not iterable

就像它刚刚决定运行第82行一样!那服务员是怎么工作的?为什么不能正常工作?怎么了


Tags: totruedatetimeobjectosmainbotprocess

热门问题