0

我有一个试图在 chroot 中运行的进程。说 chroot 是通过我没有编写的 python 脚本输入的(如果对任何人有帮助的话,它是 Chromium 的cros_sdk脚本)。这是我的代码:

def start_devserver(image_folder):
    """
    Start a devserver, serving the image in image_folder
    """
    # path manipulation stuff goes here
    devserver_command = [sdk_path, "--", devserver_exe, "--pregenerate_update",
                     "--image={}".format(image_path)]
    server_process = subprocess.Popen(devserver_command,
                                      stdout=subprocess.PIPE,
                                      stderr=subprocess.STDOUT,
                                      cwd=os.path.expanduser("~/chromiumos"))
    return server_process

def standup_devserver(image_folder):
    """
    Start the devserver and monitor its output until the update has
    been generated and the local server is ready to accept connections
    """
    devserver_process = start_devserver(image_folder)
    devserver_update_pat = re.compile(r'cache/[a-z0-9]+/update.gz')
    devserver_update = None
    devserver_started = False
    print "monitoring devserver progress"
    while not devserver_started:
        output = devserver_process.stdout.readline()
        if output:
            print output
        if output is None:
            break
        if devserver_update is None:
            match = re.search(devserver_update_pat, output)
            if match is not None:
                devserver_update = os.path.join("/var", "lib",
                                                "devserver",
                                                "static",
                                                match.group())
            LOG.info("update generated and stored in %s",
                        devserver_update)
        else:
            if "ENGINE Bus STARTED" in output:
                print "devserver started"
                devserver_started = True
    else:
        return devserver_update, devserver_process


def main(image_folder):
    """ 
    Main entry point--stand up the devserver and collect the necessary files.
    Returns the path of the temp directory containing the files
    """
    update, process = standup_devserver(image_folder)
    print "tmp_dir: {}".format(update)
    tmp_dir = collect_files(update)
    process.terminate()
    return tmp_dir

发生了两件非常奇怪的事情:

1)它分叉多个进程,当我这样做时不会被杀死server_process.terminate()

 5708 pts/8    T      0:00 python
 5712 pts/8    T      0:00 /usr/bin/python2 /.../chromiumos/chromite/bin/cros_sdk -- /.../trunk/chroot/usr/lib/devserver/devserver.py --pregenerate_update --image=/.../trunk/src/build/images/.../chromiumos_base_image.bin
 5988 pts/8    S      0:00 /usr/bin/python2 /.../chromiumos/chromite/bin/cros_sdk -- /.../trunk/chroot/usr/lib/devserver/devserver.py --pregenerate_update --image=/.../trunk/src/build/images/.../chromiumos_base_image.bin
 5993 pts/8    S      0:00 /usr/bin/python2 /.../chromiumos/chromite/bin/cros_sdk -- /.../trunk/chroot/usr/lib/devserver/devserver.py --pregenerate_update --image=/.../trunk/src/build/images/.../chromiumos_base_image.bin
 6208 pts/8    Sl     0:09 /usr/bin/python2.7 /.../trunk/chroot/usr/lib/devserver/devserver.py --pregenerate_update --image=/.../trunk/src/build/images/.../chromiumos_base_image.bin
 8322 pts/8    S+     0:00 grep --color=auto python

(省略号替换了我不想公开的部分路径,但请放心,它们在每个过程中都是相同的。)

2)当server_process返回给解释器时,解释器得到后台处理。我main的函数调用一个函数,该函数调用一个函数,该函数调用启动并返回进程的函数,但在完成之前我不会被转储到 bash 中main

sam(~/PycharmProjects/autoupdates)||$ sudo python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from local_devserver import main
>>> td = main("latest")
<snip>
devserver started
tmp_dir: /var/lib/devserver/static/cache/ae38287dde7f3f17ce03f36942d789fa/update.gz
moving update from /home/sam/chromiumos/chroot/var/lib/devserver/static/cache/ae38287dde7f3f17ce03f36942d789fa/update.gz to /tmp/tmpAX0Bm2/update.gz

[1]+  Stopped                 sudo python

subprocess如果有比我更深入了解的人可以提供一些清晰的信息,将不胜感激。

4

0 回答 0