如何正确地与父进程的子子信号工作?

问题描述 投票:0回答:1

我需要帮助的以下问题,我有一个父进程,它创建几个孩子无限POPEN子过程,也可能是情况下,当子进程不能因某些安装文件abscence工作。问题孩子子在这种情况下关闭与sys.exit它的代码内。我给信息子子压碎成父进程,在孩子问题做的过程sys.stderr.write,并要求用户重新启动手动粉碎子子(这样设置后),但我想从sys.exit赶上信号在父进程和从活动子subpocesses的词典中删除子(字典是在主过程)时sys.exit信号来。我有subprocessinstance.poll会的情况下工作猜测,但我没有尝试,但,我不知道是否有用,或者不适合我的情况。

我怎么开始的子进程,并将其添加在父进程词典:

def add_ip_to_monitoring(ip: str, ip_in_monitoring_dict: dict) -> None:
if is_ip_address(ip):
    if not is_ip_already_in_monitoring(ip, ip_in_monitoring_dict):
        if sys.platform == 'win32':
            ip_in_monitoring_dict[ip] = subprocess.Popen(["python", "pingsubprocess.py", ip],
                                                         stdout=subprocess.DEVNULL)
        elif sys.platform == 'linux':
            ip_in_monitoring_dict[ip] = subprocess.Popen(["python3", "pingsubprocess.py", ip],
                                                         stdout=subprocess.DEVNULL)
        else:
            print(f"The program is not designed to work in your OS {sys.platform}")
            print("The program will be terminated in 5 seconds, Sorry...")
            time.sleep(5)
            print("Bye")
            time.sleep(1)
            sys.exit()

        print(f"{ip} was added to monitoring\n")
        iplist_file_op.write_ip_to_file(ip)

我如何阻止孩子子:

def upload_smtp_settings():
settings = []
try:
    with open(file="settings.py", mode="r") as f:
        for i in f:
            settings.append(i.strip("\\\n"))
except FileNotFoundError:
    sys.stderr.write("Settings file does not exist or corrupted.\n\n")
    sys.stderr.write("Delete the file if it exists and do setup command\n\n")
    sys.stderr.write(f"{ip} session crushed.")
    sys.stderr.write("To restore session to the ip, remove it from monitoring, then add it again!")
    sys.stderr.flush()
    sys.exit()
return settings

链接到我的项目的详细信息,该项目的工作,但我需要给它更多的功能:https://github.com/ArtyomKozyrev1989/ICMP_Ping_Monitor_True_CLI?fbclid=IwAR2ksD70q8SksXuw9p3SEHhkk5-N46fy0lCt5jGGbLaIJaKs-dGFbBWQsfc

python python-3.x subprocess signals popen
1个回答
0
投票

我的解决方案是下面,如果任何人都可以给任何建议,欢迎你,我也知道如何可以从主发送信息给孩子当孩子已经被激活:

def check_if_all_subprocess_alive(popen_list: dict)-> dict:
if popen_list:
    popen_list_copy = popen_list.copy()
    for ip in popen_list.keys():
        if popen_list[ip].poll() is not None:
            cli_menu.remove_ip_from_monitoring(ip, popen_list_copy)
    popen_list = popen_list_copy
return popen_list

def main():
popen_list = {} # dictionary of popen subprocesses
cli_menu.hello_banner()
while True:
    command = input("CLI>: ")
    command = cli_menu.analyze_command(command)
    if command[0] in menu_wrapper.keys():
        menu_wrapper[command[0]](popen_list, command)
    elif command[0] == "FreeSpace":
        print()
    else:
        print("Incorrect command, Please try again.\n")
    popen_list = check_if_all_subprocess_alive(popen_list)
© www.soinside.com 2019 - 2024. All rights reserved.