Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43059 > unrolled thread

subprocess question re waiting

Started byloial <jldunn2000@gmail.com>
First post2013-04-08 04:00 -0700
Last post2013-04-08 08:25 -0400
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  subprocess question re waiting loial <jldunn2000@gmail.com> - 2013-04-08 04:00 -0700
    Re: subprocess question re waiting Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2013-04-08 13:48 +0200
      Re: subprocess question re waiting Dylan Evans <dylan@dje.me> - 2013-04-08 22:01 +1000
      Re: subprocess question re waiting Dave Angel <davea@davea.name> - 2013-04-08 08:22 -0400
      Re: subprocess question re waiting Dylan Evans <dylan@dje.me> - 2013-04-08 22:58 +1000
    Re: subprocess question re waiting Dave Angel <davea@davea.name> - 2013-04-08 08:25 -0400

#43059 — subprocess question re waiting

Fromloial <jldunn2000@gmail.com>
Date2013-04-08 04:00 -0700
Subjectsubprocess question re waiting
Message-ID<ed0010ee-471d-4cbe-ae6c-1d9a172b2ffc@googlegroups.com>
I want to call a child process to run a shell script and wait for that script to finish. Will the code below wait for the script to finish? If not then how do I make it wait?

Any help appreciated.


import subprocess

command = "/home/john/myscript"

process = subprocess.Popen(command, stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, shell=True)

out, err = process.communicate()
returncode = process.returncode

[toc] | [next] | [standalone]


#43062

FromAlain Ketterlin <alain@dpt-info.u-strasbg.fr>
Date2013-04-08 13:48 +0200
Message-ID<8761zxdyzb.fsf@dpt-info.u-strasbg.fr>
In reply to#43059
loial <jldunn2000@gmail.com> writes:

> I want to call a child process to run a shell script and wait for that
> script to finish. Will the code below wait for the script to finish?
> If not then how do I make it wait?
[...]
> process = subprocess.Popen(command, stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, shell=True)

process.wait()

-- Alain.

[toc] | [prev] | [next] | [standalone]


#43064

FromDylan Evans <dylan@dje.me>
Date2013-04-08 22:01 +1000
Message-ID<mailman.284.1365422522.3114.python-list@python.org>
In reply to#43062

[Multipart message — attachments visible in raw view] — view raw

On Mon, Apr 8, 2013 at 9:48 PM, Alain Ketterlin <alain@dpt-info.u-strasbg.fr
> wrote:

> loial <jldunn2000@gmail.com> writes:
>
> > I want to call a child process to run a shell script and wait for that
> > script to finish. Will the code below wait for the script to finish?
> > If not then how do I make it wait?
> [...]
> > process = subprocess.Popen(command,
> stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE,
> close_fds=True, shell=True)
>
> process.wait()
>

Or use subprocess.call instead which does what you want.


> -- Alain.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
"The UNIX system has a command, nice ... in order to be nice to the other
users. Nobody ever uses it." - Andrew S. Tanenbaum

[toc] | [prev] | [next] | [standalone]


#43065

FromDave Angel <davea@davea.name>
Date2013-04-08 08:22 -0400
Message-ID<mailman.285.1365423791.3114.python-list@python.org>
In reply to#43062
On 04/08/2013 08:01 AM, Dylan Evans wrote:
> On Mon, Apr 8, 2013 at 9:48 PM, Alain Ketterlin <alain@dpt-info.u-strasbg.fr
>> wrote:
>
>> loial <jldunn2000@gmail.com> writes:
>>
>>> I want to call a child process to run a shell script and wait for that
>>> script to finish. Will the code below wait for the script to finish?
>>> If not then how do I make it wait?
>> [...]
>>> process = subprocess.Popen(command,
>> stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE,
>> close_fds=True, shell=True)
>>
>> process.wait()
>>
>
> Or use subprocess.call instead which does what you want.
>
>
>> -- Alain.
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>

http://docs.python.org/2/library/subprocess.html#popen-objects

or use communicate(), which is what the OP had in the first place.


-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#43069

FromDylan Evans <dylan@dje.me>
Date2013-04-08 22:58 +1000
Message-ID<mailman.288.1365425904.3114.python-list@python.org>
In reply to#43062

[Multipart message — attachments visible in raw view] — view raw

On Mon, Apr 8, 2013 at 10:22 PM, Dave Angel <davea@davea.name> wrote:

> On 04/08/2013 08:01 AM, Dylan Evans wrote:
>
>> On Mon, Apr 8, 2013 at 9:48 PM, Alain Ketterlin <
>> alain@dpt-info.u-strasbg.fr
>>
>>> wrote:
>>>
>>
>>  loial <jldunn2000@gmail.com> writes:
>>>
>>>  I want to call a child process to run a shell script and wait for that
>>>> script to finish. Will the code below wait for the script to finish?
>>>> If not then how do I make it wait?
>>>>
>>> [...]
>>>
>>>> process = subprocess.Popen(command,
>>>>
>>> stdin=subprocess.PIPE,stdout=**subprocess.PIPE, stderr=subprocess.PIPE,
>>> close_fds=True, shell=True)
>>>
>>> process.wait()
>>>
>>>
>> Or use subprocess.call instead which does what you want.
>>
>>
Actually after having a look through the manual i like check_output for
this since it simplifies the code, but some extra exception handling would
be required if the output is still required when the script exits with a
non zero value, so it's a bit of a trade off.


>  -- Alain.
>>> --
>>> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>>>
>>>
>>
> http://docs.python.org/2/**library/subprocess.html#popen-**objects<http://docs.python.org/2/library/subprocess.html#popen-objects>
>
> or use communicate(), which is what the OP had in the first place.
>
>
> --
> DaveA
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>



-- 
"The UNIX system has a command, nice ... in order to be nice to the other
users. Nobody ever uses it." - Andrew S. Tanenbaum

[toc] | [prev] | [next] | [standalone]


#43066

FromDave Angel <davea@davea.name>
Date2013-04-08 08:25 -0400
Message-ID<mailman.286.1365423942.3114.python-list@python.org>
In reply to#43059
On 04/08/2013 07:00 AM, loial wrote:
> I want to call a child process to run a shell script and wait for that script to finish. Will the code below wait for the script to finish? If not then how do I make it wait?
>
> Any help appreciated.
>
>
> import subprocess
>
> command = "/home/john/myscript"
>
> process = subprocess.Popen(command, stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, shell=True)
>
> out, err = process.communicate()
> returncode = process.returncode
>

Yes, communicate() will block until the child process is complete.

   http://docs.python.org/2/library/subprocess.html#popen-objects

Note the phrase:  "Wait for process to terminate."  That's referring to 
the shell in your case.

-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web