[Tutor] Understand subprocess poll
Peter Otten
__peter__ at web.de
Tue Sep 9 16:18:40 CEST 2014
Wolfgang Maier wrote:
> On 09/09/2014 11:45 AM, Peter Otten wrote:
>> jarod_v6 at libero.it wrote:
>>
>>> I want to use subprocess for run some programs But I need to be sure the
>>> program end before continue with the other:
>>>
>>> subprocess.call("ls")
>>> cmd1 = i
>>> p1 = subprocess.Popen(cmd1,shell=True,stdout=subprocess.PIPE)
>>>
>>> while True:
>>> if p1.poll() is None:
>>> time.sleep(3)
>>>
>>> pass
>>> if p1.poll()==0:
>>> print '#'
>>> break
>>> if p1.poll() is not None and p1.poll() != 0:
>>> raise Exception('Error building Alignment using star with hg19
>>> database')
>>
>>> This are not working. How can I do?
>>> thanks in advance for the precious help
>>> bw,
>>
>> I don't understand why you would need this loop. Why don't you use
>> subprocess.call() and be done?
>>
>
> The OP is piping the process stdout so I assume he is going to read from
> it in place of the pass in his example.
> Since the subprocess is doing genome-wide sequence alignment (at least I
> guess so from the exception string) there will be lots of output, which
> would cause subprocess.call() to block.
>
> Assuming that the posted code was indented correctly and was otherwise
> run as posted this could also be the answer to the original question:
> you have to keep on consuming data from the pipe or its buffer is going
> to fill up and block everyhing. With a simple pass statement you do not
> achieve anything that you can't do with call.
Ah, you're right.
I still don't see where the need to poll arises, I'd expect something like
p = subprocess.Popen(cmd, ..., stdout=PIPE)
for line in p.stdout:
process(line)
p.stdout.close()
if p.wait() != 0:
raise Exception
to work.
More information about the Tutor
mailing list