Python子进程:cmd退出时的回调 [英] Python subprocess: callback when cmd exits

查看:1424
本文介绍了Python子进程:cmd退出时的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 subprocess.Popen(cmd,shell = TRUE)启动程序



对于Python来说是相当新的,但是它觉得应该有一些api让我做类似的事情:

  subprocess.Popen(cmd,shell = TRUE,postexec_fn = function_to_call_on_exit)

function_to_call_on_exit 可以根据知道cmd已退出(例如保持当前正在运行的外部进程的数量)来执行某些操作。



假设我可以在一个类中使用 Popen.wait()方法合并子进程,但是由于我没有完成线程Python还有,似乎这可能是普通的API存在,我想我会尝试,并找到一个。



提前感谢:)


解决方案

你说得对 - 没有很好的API。你也是正确的第二点 - 设计一个功能,这是为你使用线程很容易。

  import线程
import subprocess

def popenAndCall(onExit,popenArgs):

在子进程.Popen中运行给定的参数,然后调用函数
onExit当子进程完成
onExit是一个可调用对象,popenArgs是一个列表/元组的args,
将给予subprocess.Popen。

def runInThread(onExit,popenArgs):
proc = subprocess.Popen(* popenArgs)
proc.wait()
onExit()
return
thread = threading .Thread(target = runInThread,args =(onExit,popenArgs))
thread.start()
#在线程启动后立即返回
返回线程

即使线程在Python中也很容易,但是请注意,如果onExit()在计算上是昂贵的,你需要把它放在一个单独的进程而不是使用多处理(使GIL不会减慢你的程序)。它实际上很简单 - 你可以基本上只是替换 threading.Thread multiprocessing.Process 的所有调用,因为他们遵循几乎)相同的API。


I'm currently launching a programme using subprocess.Popen(cmd, shell=TRUE)

I'm fairly new to Python, but it 'feels' like there ought to be some api that lets me do something similar to:

subprocess.Popen(cmd, shell=TRUE,  postexec_fn=function_to_call_on_exit)

I am doing this so that function_to_call_on_exit can do something based on knowing that the cmd has exited (for example keeping count of the number of external processes currently running)

I assume that I could fairly trivially wrap subprocess in a class that combined threading with the Popen.wait() method, but as I've not done threading in Python yet and it seems like this might be common enough for an API to exist, I thought I'd try and find one first.

Thanks in advance :)

解决方案

You're right - there is no nice API for this. You're also right on your second point - it's trivially easy to design a function that does this for you using threading.

import threading
import subprocess

def popenAndCall(onExit, popenArgs):
    """
    Runs the given args in a subprocess.Popen, and then calls the function
    onExit when the subprocess completes.
    onExit is a callable object, and popenArgs is a list/tuple of args that 
    would give to subprocess.Popen.
    """
    def runInThread(onExit, popenArgs):
        proc = subprocess.Popen(*popenArgs)
        proc.wait()
        onExit()
        return
    thread = threading.Thread(target=runInThread, args=(onExit, popenArgs))
    thread.start()
    # returns immediately after the thread starts
    return thread

Even threading is pretty easy in Python, but note that if onExit() is computationally expensive, you'll want to put this in a separate process instead using multiprocessing (so that the GIL doesn't slow your program down). It's actually very simple - you can basically just replace all calls to threading.Thread with multiprocessing.Process since they follow (almost) the same API.

这篇关于Python子进程:cmd退出时的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆