Reputation: 1261
Please go through the python code snippet below
import subprocess
child = subprocess.Popen("python ./myprog.py ", shell=True)
print "Hello"
Where myprog.py is simply :print "Hello,myprog"
The output I get is :
Hello
Hello,myprog
My question is whether the child process starts running parallel to the parent process or does the child waits for the parent process to finish off,as the above output suggests?
Upvotes: 1
Views: 1840
Reputation: 363
According to python docs http://docs.python.org/library/subprocess.html
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
Run the command described by args. Wait for command to complete, then return the returncode attribute.
So it appears that the spawn process is a child process for the python script itself.
Upvotes: 0
Reputation: 1360
disconnecting a subprocess is the same as creating a daemon. See http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/
As to your original question, the subprocess at least starts executing before the next statement. Note that you will need to process the stdout of that subprocess to actually get it to print.
Upvotes: 0
Reputation: 2041
To my understanding, it is launched as another process in the same fashion as a Unix fork() or whatever the Windows equivalent is.
And then there's this: http://docs.python.org/library/subprocess.html#subprocess.Popen.wait
Observe:
>>> 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
So, in essence, yes, it's running in parallel.
Upvotes: 3