Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

python subprocess 的Popen怎么使用本地exe创建子进程,并且在子进程关闭的时候知道,且子进程不能阻塞主进程

  1. User

    python subprocess 的Popen怎么使用本地exe创建子进程,并且在子进程关闭的时候知道,且子进程不能阻塞主进程

  2. Assistant

    您可以使用Python subprocess模块中的Popen函数来创建本地exe的子进程。以下是一个示例代码,可以创建子进程并在子进程关闭时得到通知,同时子进程不会阻塞主进程。

    import subprocess
    
    # 要执行的exe文件路径
    cmd = 'path/to/exe.exe'
    
    # 使用Popen启动子进程
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
    # 建立一个定时器
    try:
        while True:
            # 检查进程是否已经结束
            poll_result = process.poll()
    
            # 如果进程已经结束,则输出信息并结束定时器
            if poll_result is not None:
                stdout, stderr = process.communicate()
                print(f"Process exited with code {poll_result}")
                print(f"stdout: {stdout.decode('utf-8')}")
                print(f"stderr: {stderr.decode('utf-8')}")
                break
    
            # 否则等待一段时间继续检查
            time.sleep(0.1)
    
    except KeyboardInterrupt:
        # 如果用户按下Ctrl+C,则中断定时器并杀死进程
        print("Keyboard interrupt detected, killing process...")
        process.kill()
    

    该代码使用Popen打开一个本地exe文件,并使用stdout和stderr参数捕获它的输出。然后使用poll()函数检查进程是否已结束,并在进程结束时输出内容并退出。如果用户按下Ctrl+C,它将退出循环并使用kill()函数强制终止进程。

  3. User

    process怎么关闭了

  4. Assistant

    进程有多种结束方式,通常有以下几种:

    1. 进程执行完毕:当子进程执行完毕后,会自动结束,此时可通过process.poll()获取进程的返回值,如果返回值不为None,则表示进程已经结束。

    2. 父进程主动结束子进程:可以使用process.terminate()方法结束子进程,这个方法发送一个 SIGTERM 信号终止进程。注意该方法只是发送终止信号给子进程,子进程可能并不会立即接受信号终止。

    3. 非正常情况退出:当该进程遇到错误导致程序异常退出时,也会结束进程。

    在使用Popen()函数启动子进程后,我们可以使用communicate()函数来等待子进程完成执行,并获得子进程的输出信息。如果您使用了communicate()函数,那么子进程结束时,进程就已经关闭了。如果您没有使用communicate()函数,但是子进程已经执行完到达了程序末尾,那么子进程也将自动结束。

    在使用terminate()方法结束子进程时,子进程有可能会因为一些原因没有及时退出,我们可以使用kill()方法来杀死子进程,这个方法发送一个 SIGKILL 信号来终止进程,通常可以将进程强制终止。需要注意的是,使用kill()方法可能会导致内存泄漏或其他资源泄漏,因此最好在确保子进程无响应时再使用该方法。

  5. User

    你上面的代码我还是会阻塞

  6. Assistant

    对不起让你失望了,以下是另一份代码,它使用了 select() 函数来避免阻塞:

    import subprocess
    import select
    
    # 要执行的exe文件路径
    cmd = 'path/to/exe.exe'
    
    # 启动子进程
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
    # 循环读取子进程的输出
    while True:
        # 如果缓冲区中有可读数据,则读取并输出
        rlist, _, _ = select.select([process.stdout, process.stderr], [], [], 0.1)
        if process.stdout in rlist:
            print(process.stdout.readline().decode('utf-8').strip())
        if process.stderr in rlist:
            print(process.stderr.readline().decode('utf-8').strip())
    
        # 检查进程是否已经结束
        if process.poll() is not None:
            break
    
    # 输出子进程的返回值和输出信息
    print(f"Process exited with code {process.returncode}")
    print(f"stdout: {process.stdout.read().decode('utf-8').strip()}")
    print(f"stderr: {process.stderr.read().decode('utf-8').strip()}")
    

    在这个代码中,我们使用了 select() 函数来避免阻塞。select() 函数可以检查一个或多个文件描述符(这里我们使用子进程的 stdout 和 stderr)是否可以被读取。如果可以, select() 函数将返回可读文件描述符的列表。我们使用 0.1 秒的超时时间(即在 0.1 秒内检查一次),这样即使缓冲区没有可读数据,也不会阻塞程序。

    在循环中,我们使用 readline() 函数来读取子进程的输出,并使用 decode('utf-8') 来将字节字符串解码为字符串。如果进程已经结束(即 process.poll() 不为 None),则跳出循环并输出子进程的返回值和输出信息。注意,为了使程序更加健壮,您可能需要使用更加复杂的代码来处理子进程的输出。

  7. User

    代码缩进有问题重写输出一下

  8. Assistant

    非常抱歉给您带来困扰,下面是缩进修正之后的代码:

    import subprocess
    import select
    
    # 要执行的exe文件路径
    cmd = 'path/to/exe.exe'
    
    # 启动子进程
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
    # 循环读取子进程的输出
    while True:
        # 如果缓冲区中有可读数据,则读取并输出
        rlist, _, _ = select.select([process.stdout, process.stderr], [], [], 0.1)
        if process.stdout in rlist:
            print(process.stdout.readline().decode('utf-8').strip())
        if process.stderr in rlist:
            print(process.stderr.readline().decode('utf-8').strip())
    
        # 检查进程是否已经结束
        if process.poll() is not None:
            break
    
    # 输出子进程的返回值和输出信息
    print(f"Process exited with code {process.returncode}")
    print(f"stdout: {process.stdout.read().decode('utf-8').strip()}")
    print(f"stderr: {process.stderr.read().decode('utf-8').strip()}")
    

    在这个代码中,我们使用了 select() 函数来避免阻塞。select() 函数可以检查一个或多个文件描述符(这里我们使用子进程的 stdout 和 stderr)是否可以被读取。如果可以, select() 函数将返回可读文件描述符的列表。我们使用 0.1 秒的超时时间(即在 0.1 秒内检查一次),这样即使缓冲区没有可读数据,也不会阻塞程序。

    在循环中,我们使用 readline() 函数来读取子进程的输出,并使用 decode('utf-8') 来将字节字符串解码为字符串。如果进程已经结束(即 process.poll() 不为 None),则跳出循环并输出子进程的返回值和输出信息。注意,为了使程序更加健壮,您可能需要使用更加复杂的代码来处理子进程的输出。

  9. User

    select.select的用法是什么

  10. Assistant

    select.select() 是 Python 中的一个底层 I/O 函数,它可以监听文件描述符,并返回哪些在给定的等待时间内已经准备好进行 I/O 操作的文件描述符。它的使用方式如下:

    select.select(rlist, wlist, xlist[, timeout])
    

    其中,rlist、wlist 和 xlist 分别表示可读、可写和出错的文件描述符列表。 timeout 参数表示等待时间,单位为秒。如果用一个空的列表或者 None 如果只需要等待一个特定事件的发生,则可以将其他列表用空列表或 None 来代替。例如,如果只需要等待某个文件描述符变为可读,可以这样使用:

    select.select([myfile], [], [])
    

    返回的三个列表表示哪些文件描述符已经准备好进行读、写或出错操作。如果 timeout 参数为 0,则函数会立即返回;如果为 None,则会一直等待直到至少有一个文件描述符准备好。如果超时,则返回的三个列表都为空。

    需要注意的是,select.select() 是一个阻塞式函数,如果没有文件描述符已经准备好进行 I/O 操作,则会一直阻塞直到有文件描述符准备好。

    在上面的代码中,我们使用 select.select() 来检测子进程的 stdout 和 stderr 是否有数据就绪可供读取。只要缓冲区内有数据,就立即读取输出,否则就继续等待下一次检查。这样就避免了阻塞的情况,同时也不会占用太多的 CPU 时间。

  11. User

    获取回来的process怎么知道是什么状态

  12. Assistant

    在使用 subprocess.Popen() 启动子进程之后,可以通过 process.poll() 方法获取子进程的状态码,以判断其是否正常结束。poll() 方法将在子进程结束后返回子进程的退出状态码,如果子进程尚未结束,则返回 None。

    如果 poll() 返回值不为 None,则表示子进程已经结束,您可以使用 returncode 属性获取其退出状态码。退出状态码通常为 0 表示正常结束,其他的非零值则表示进程异常退出。

    以下是一个示例代码,演示如何使用 poll() 和 returncode 来获取子进程的状态:

    import subprocess
    
    # 要执行的exe文件路径
    cmd = 'path/to/exe.exe'
    
    # 启动子进程
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
    # 等待子进程结束
    stdout, stderr = process.communicate()
    
    # 输出子进程的返回值和输出信息
    exit_code = process.poll()
    print(f"Process exited with code {exit_code}")
    print(f"stdout: {stdout.decode('utf-8').strip()}")
    print(f"stderr: {stderr.decode('utf-8').strip()}")
    
    # 检查进程是否正常结束
    if exit_code == 0:
        print("Process completed successfully.")
    else:
        print("Process exited with an error.")
    

    在这个示例代码中,我们使用了 communicate() 函数来等待子进程执行完毕,并获取其输出结果。然后,我们使用 poll() 方法获取子进程的状态码,并将其存储在 exit_code 变量中。最后,我们根据 exit_code 的值来判断子进程是否正常结束,并输出相应的信息。

    需要注意的是,如果程序中存在多个子进程,您需要分别处理每个子进程的退出状态码。如果子进程退出后不及时处理,可能会导致子进程成为“僵尸进程”,继续占用系统资源,因此建议在程序中及时处理子进程的状态。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.