我有一个我正试图在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
函数调用一个函数来调用一个函数,该函数调用启动的函数&返回进程,但在main
完成之前我不会被抛弃到bash中:
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
比我更深入了解的人能够提供一些清晰度,那么我们将非常感激。