2011-11-15 69 views
1

請通過Python代碼片段下面去的Python subprocess.Popen()

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

哪裏myprog.py很簡單:
print "Hello,myprog"

我得到的輸出是:

Hello 
Hello,myprog 

我問題是子進程是否開始平行於父進程運行,或者子進程是否等待父進程完成,如上面的輸出所示?

+0

我碰巧爲類似的帖子做了一個答案:http://stackoverflow.com/questions/89228/how-to-call-external-command-in-python/2251026#2251026 – newtover

回答

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

現在的問題是,我怎麼能斷開孩子與父母的關係? – PaulDaviesC

0

根據python文檔http://docs.python.org/library/subprocess.html

subprocess.call(參數,*,標準輸入=無,標準輸出=無,標準錯誤=無,殼=假)
運行由ARGS中描述的命令。 等待命令完成,然後返回returncode屬性。

因此,似乎spawn進程是python腳本本身的子進程。