Python subprocess.Popen()

时间:2011-11-15 03:02:37

标签: python process

请浏览下面的python代码段

import subprocess  
child = subprocess.Popen("python ./myprog.py ", shell=True)  
print "Hello"

myprog.py只是:print "Hello,myprog"

我得到的输出是:

Hello
Hello,myprog

我的问题是子进程是否开始与父进程并行运行,或者子进程是否等待父进程完成,如上面的输出所示?

3 个答案:

答案 0 :(得分:3)

据我所知,它是以与Unix fork()或Windows等价物相同的方式作为另一个进程启动的。

然后就是:http://docs.python.org/library/subprocess.html#subprocess.Popen.wait

观察:

>>> p = Popen(["ls", "-hal"]);sleep (5);print "Word"
total 12M
drwxr-xr-x 51 ishpeck users   4.0K Nov 14 19:58 .
drwxr-xr-x  7 root    root    4.0K Jun 25 04:30 ..
drwxr-xr-x  2 ishpeck users   4.0K Jun  6 00:46 abcde.b00b030d
drwx------  2 ishpeck users   4.0K Nov 13 13:57 .AbiSuite
drwx------  3 ishpeck users   4.0K May 29 20:14 .adobe
-rw-r--r--  1 ishpeck archive   55 Jun 29 07:18 .apvlvinfo
drwxr-xr-x  2 ishpeck archive 4.0K Jul 28 23:16 .archive
-rw-r--r--  1 ishpeck archive  180 Nov 14 18:23 .aspell.en.prepl
-rw-r--r--  1 ishpeck archive  250 Nov 14 18:23 .aspell.en.pws
-rw-r--r--  1 ishpeck users    152 Jun  6 00:45 .asunder
-rw-------  1 ishpeck users   9.0K Nov 14 20:06 .bash_history
-rw-r--r--  1 ishpeck users   2.2K Oct 22 08:54 .bashrc
drwx------  3 ishpeck users   4.0K Sep 28 08:23 .bitcoin
drwx------  3 ishpeck archive 4.0K Jul 30 22:44 .Broken Rules
drwx------ 10 ishpeck users   4.0K Aug 16 16:37 .cache
drwx------  2 ishpeck users   4.0K May 30 03:42 .cmus
drwxr-xr-x 11 ishpeck users   4.0K Nov  6 16:42 .codelite
drwx------ 23 ishpeck users   4.0K Sep 22 00:18 .config
-rw-r--r--  1 ishpeck users   2.6K Jun  5 17:21 .conkyrc
drwx------  3 ishpeck users   4.0K May 28 08:29 .dbus
drwx------ 13 ishpeck users   4.0K Sep 27 21:25 dl
drwxr-xr-x 29 ishpeck users   4.0K Nov 12 00:00 docs
-rw-r--r--  1 ishpeck archive 1000 Nov 11 06:26 .emacs
-rw-r--r--  1 ishpeck archive 1000 Nov 11 06:25 .emacs~
-rw-r--r--  1 ishpeck archive  511 Nov 10 11:14 .emacs-backup
drwx------  6 ishpeck users   4.0K Nov 10 11:22 .emacs.d
drwxr-xr-x  3 ishpeck archive 4.0K Jun 17 04:26 .Eterm
-rw-r--r--  1 ishpeck users    105 Nov 14 20:00 .fehbg
-rw-------  1 ishpeck archive    9 Nov 13 22:10 .fetchmail.pid
-rw-------  1 ishpeck archive  323 Oct 30 08:10 .fetchmailrc
drwxr-xr-x  2 ishpeck users   4.0K Nov  2 00:43 .fontconfig
drwx------  4 ishpeck users   4.0K Nov 13 22:10 .gconf
drwx------  2 ishpeck users   4.0K Oct  1 20:50 .gconfd
-rw-r--r--  1 ishpeck archive 127K Nov  7 10:19 geany-colors.tar.gz
drwxr-xr-x  2 ishpeck users   4.0K May 31 05:43 .ghc
drwxr-xr-x 22 ishpeck archive 4.0K Nov 14 18:24 .gimp-2.6
-rw-r--r--  1 ishpeck users     75 May 31 12:59 .gitconfig
-rw-r--r--  1 ishpeck archive  893 Oct 12 20:03 .gitk
drwx------  3 ishpeck archive 4.0K Jul 30 19:46 .gnome
drwx------  3 ishpeck users   4.0K Jul  1 13:37 .gnome2
drwx------  2 ishpeck archive 4.0K Jul  1 13:37 .gnome2_private
drwxr-xr-x  2 ishpeck users   4.0K Nov  2 19:05 .gstreamer-0.10
-rw-------  1 ishpeck users    234 Nov  7 20:36 .gtk-bookmarks
-rw-r--r--  1 ishpeck users    164 Jun  1 04:10 .gtkrc-2.0
drwx------  2 ishpeck users   4.0K May 30 03:35 .gvfs
drwxr-xr-x  3 ishpeck users   4.0K May 28 15:51 .icons
drwxr-xr-x  2 ishpeck archive 4.0K Aug 11 06:59 .idlerc
drwx------  2 ishpeck archive 4.0K Jul 16 19:36 .irssi
-rw-------  1 ishpeck users    846 Nov 11 20:09 .lesshst
drwx------  3 ishpeck users   4.0K Nov 11 22:35 .liferea_1.6
drwx------  3 ishpeck users   4.0K May 28 06:12 .local
-rw-r--r--  1 ishpeck users    842 Jun  5 16:34 lynx_bookmarks.html
-rw-------  1 ishpeck users   3.3K Jun 11 04:14 Lynx.trace
drwx------  3 ishpeck users   4.0K May 29 20:14 .macromedia
drwx------  2 ishpeck users   4.0K Sep  2 19:42 Mail
drwxr-xr-x  5 ishpeck users   4.0K Nov  7 07:47 media
-rw-r--r--  1 ishpeck archive 3.8K Jul 16 22:56 .mrxvtrc
drwxr-xr-x  2 ishpeck archive 4.0K Sep 14 08:12 .mutt
-rw-r--r--  1 ishpeck archive 3.3K Oct 14 06:32 .muttrc
-rw-r--r--  1 ishpeck archive 1.3K Jul 27 22:36 .nvidia-settings-rc
drwx------  3 ishpeck users   4.0K May 28 08:35 .pki
-rw-------  1 ishpeck archive    0 Nov  9 07:24 postponed
drwxr-xr-x 12 ishpeck users   4.0K Sep 28 08:08 project
drwx------  2 ishpeck archive 4.0K Nov 14 18:07 .pulse
-rw-------  1 ishpeck users    256 May 31 03:52 .pulse-cookie
drwx------  6 ishpeck users   4.0K Nov 14 20:15 .purple
-rw-r--r--  1 ishpeck users    12K Nov  2 07:06 .pypanelrc
drwxr-xr-x  3 ishpeck users   4.0K Jun 11 02:29 .q3a
-rw-r--r--  1 ishpeck archive 1.9K Aug 23 17:11 .recently-used
drwxr-xr-x  2 ishpeck users   4.0K Nov  2 07:55 .sbin
-    rw-------  1 ishpeck archive  11M Nov 14 19:52 sent
-rw-r--r--  1 ishpeck archive   61 Jul  2 21:44 .signature
drwxr-xr-x  3 ishpeck archive 4.0K Nov  5 07:43 .slime
drwx------  2 ishpeck users   4.0K Oct 31 22:46 .ssh
drwxr-xr-x  3 ishpeck archive 4.0K Aug  6 16:49 .steelstorm
drwxr-xr-x  3 ishpeck archive 4.0K Jul 28 23:16 .subversion
-rw-r--r--  1 ishpeck archive  273 Jun 18 02:47 .synergy.conf
drwxr-xr-x  9 ishpeck users   4.0K Jun  1 04:10 .themes
drwx------  3 ishpeck users   4.0K May 28 09:39 .thumbnails
drwxr-xr-x  2 ishpeck users   4.0K Oct 25 06:36 .vim
-rw-------  1 ishpeck archive  19K Nov 14 19:52 .viminfo
-rw-r--r--  1 ishpeck users     65 Jun 17 22:30 .vimrc
drwxr-xr-x  2 ishpeck archive 4.0K Nov 13 23:26 .VirtualBox
drwxr-xr-x  3 ishpeck archive 4.0K Jul 30 19:52 .vvvvvv
-rw-------  1 ishpeck archive  101 Aug 17 05:51 .Xauthority
-rw-r--r--  1 ishpeck archive  134 Aug 19 06:32 .Xdefaults
-rw-r--r--  1 ishpeck users    120 Sep 26 21:19 .xinitrc
-rw-r--r--  1 ishpeck archive 7.6K Oct 13 07:23 .xscreensaver
Word

所以,实质上,是的,它是并行运行的。

答案 1 :(得分:0)

断开子进程与创建守护进程相同。见http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/

至于你原来的问题,子进程至少在下一个语句之前开始执行。请注意,您需要处理该子流程的标准输出以实际打印它。

答案 2 :(得分:0)

根据python docs http://docs.python.org/library/subprocess.html

subprocess.call(args,*,stdin = None,stdout = None,stderr = None,shell = False)
运行args描述的命令。 等待命令完成,然后返回returncode属性。

因此,似乎spawn进程是python脚本本身的子进程。