<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Troll&#039;s factory &#187; popen</title>
	<atom:link href="http://trollfactory.fr/tag/popen/feed" rel="self" type="application/rss+xml" />
	<link>http://trollfactory.fr</link>
	<description>Geekeries &#38; pensées</description>
	<lastBuildDate>Mon, 09 Jun 2014 19:43:25 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.41</generator>
	<item>
		<title>Output Popen subprocess stdout (and stderr) in real-time in Python</title>
		<link>http://trollfactory.fr/output-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731</link>
		<comments>http://trollfactory.fr/output-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731#comments</comments>
		<pubDate>Mon, 09 Jun 2014 19:43:25 +0000</pubDate>
		<dc:creator><![CDATA[Troll]]></dc:creator>
				<category><![CDATA[astuces]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Scripts, astuces, dév. web]]></category>
		<category><![CDATA[popen]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[subprocess]]></category>

		<guid isPermaLink="false">http://trollfactory.fr/?p=731</guid>
		<description><![CDATA[So you&#8217;re writing a Python script and you are launching some other external command. To achieve that, you are using the subprocess.Popen command, right. But the subprocess is very slow, and it outputs data during it runtime, so you would like to output this data to the user as well, so that the user does [...]]]></description>
				<content:encoded><![CDATA[<p>So you&rsquo;re writing a Python script and you are launching some other external command.</p>
<p>To achieve that, you are using the <b>subprocess.Popen</b> command, right.</p>
<p>But the subprocess is very slow, and it outputs data during it runtime, so you would like to output this data to the user as well, so that the user does not wait for the end of the subprocess before getting the information?</p>
<p>Example of a slow subprocess:</p>
<pre>
#!/bin/bash
for i in 1 2 3 4 5 6; do sleep 5 &#038;&#038; echo "${i}th output" &#038;&#038; echo "err output num ${i}" >&#038;2; done
</pre>
<p>Well, that&rsquo;s fairly easy, just redirect the subprocess&rsquo;s stdout to your own stdout:</p>
<pre>
import sys
from subprocess import Popen
Popen("./slow_cmd_output.sh", stdout=sys.stdout, stderr=sys.stderr).communicate()
</pre>
<p>Although the catch here is the <b>.communicate()</b>. This will allow your program to wait for the end of the subprocess before executing the next line of code. So that your programm does not terminate execution before its subprocess, for instance.</p>
<p>Not that if you want to deamonize the script / zombify it, you can remove the <b>.communicate()</b> and the redirection of stdout and stderr will still work after your program&rsquo;s termination.</p>
Share and Enjoy:<a rel="nofollow" target="_blank"  href="http://www.printfriendly.com/print/new?url=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/printfriendly.png" class="sociable-img sociable-hovers" title="Print" alt="Print" /></a><a rel="nofollow" target="_blank"  href="http://www.printfriendly.com/print/new?url=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/pdf.png" class="sociable-img sociable-hovers" title="PDF" alt="PDF" /></a><a rel="nofollow" target="_blank"  href="http://twitter.com/home?status=Output%20Popen%20subprocess%20stdout%20%28and%20stderr%29%20in%20real-time%20in%20Python%20-%20http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/twitter.png" class="sociable-img sociable-hovers" title="Twitter" alt="Twitter" /></a><a rel="nofollow" target="_blank"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731&amp;t=Output%20Popen%20subprocess%20stdout%20%28and%20stderr%29%20in%20real-time%20in%20Python" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/facebook.png" class="sociable-img sociable-hovers" title="Facebook" alt="Facebook" /></a><a rel="nofollow" target="_blank"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731&amp;title=Output%20Popen%20subprocess%20stdout%20%28and%20stderr%29%20in%20real-time%20in%20Python&amp;source=The+Troll%26%23039%3Bs+factory+Geekeries+%26amp%3B+pens%C3%A9es&amp;summary=So%20you%27re%20writing%20a%20Python%20script%20and%20you%20are%20launching%20some%20other%20external%20command.%0D%0A%0D%0ATo%20achieve%20that%2C%20you%20are%20using%20the%20subprocess.Popen%20command%2C%20right.%0D%0A%0D%0ABut%20the%20subprocess%20is%20very%20slow%2C%20and%20it%20outputs%20data%20during%20it%20runtime%2C%20so%20you%20would%20like%20t" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/linkedin.png" class="sociable-img sociable-hovers" title="LinkedIn" alt="LinkedIn" /></a><a rel="nofollow" target="_blank"  href="http://trollfactory.fr/feed" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/rss.png" class="sociable-img sociable-hovers" title="RSS" alt="RSS" /></a><a rel="nofollow" target="_blank"  href="http://www.wikio.fr/vote?url=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/wikio.png" class="sociable-img sociable-hovers" title="Wikio FR" alt="Wikio FR" /></a><a rel="nofollow" target="_blank"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731&amp;title=Output%20Popen%20subprocess%20stdout%20%28and%20stderr%29%20in%20real-time%20in%20Python&amp;bodytext=So%20you%27re%20writing%20a%20Python%20script%20and%20you%20are%20launching%20some%20other%20external%20command.%0D%0A%0D%0ATo%20achieve%20that%2C%20you%20are%20using%20the%20subprocess.Popen%20command%2C%20right.%0D%0A%0D%0ABut%20the%20subprocess%20is%20very%20slow%2C%20and%20it%20outputs%20data%20during%20it%20runtime%2C%20so%20you%20would%20like%20t" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/digg.png" class="sociable-img sociable-hovers" title="Digg" alt="Digg" /></a><a rel="nofollow" target="_blank"  href="http://delicious.com/post?url=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731&amp;title=Output%20Popen%20subprocess%20stdout%20%28and%20stderr%29%20in%20real-time%20in%20Python&amp;notes=So%20you%27re%20writing%20a%20Python%20script%20and%20you%20are%20launching%20some%20other%20external%20command.%0D%0A%0D%0ATo%20achieve%20that%2C%20you%20are%20using%20the%20subprocess.Popen%20command%2C%20right.%0D%0A%0D%0ABut%20the%20subprocess%20is%20very%20slow%2C%20and%20it%20outputs%20data%20during%20it%20runtime%2C%20so%20you%20would%20like%20t" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/delicious.png" class="sociable-img sociable-hovers" title="del.icio.us" alt="del.icio.us" /></a><a rel="nofollow" target="_blank"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731&amp;title=Output%20Popen%20subprocess%20stdout%20%28and%20stderr%29%20in%20real-time%20in%20Python&amp;annotation=So%20you%27re%20writing%20a%20Python%20script%20and%20you%20are%20launching%20some%20other%20external%20command.%0D%0A%0D%0ATo%20achieve%20that%2C%20you%20are%20using%20the%20subprocess.Popen%20command%2C%20right.%0D%0A%0D%0ABut%20the%20subprocess%20is%20very%20slow%2C%20and%20it%20outputs%20data%20during%20it%20runtime%2C%20so%20you%20would%20like%20t" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/googlebookmark.png" class="sociable-img sociable-hovers" title="Google Bookmarks" alt="Google Bookmarks" /></a><a rel="nofollow" target="_blank"  href="http://technorati.com/faves?add=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/technorati.png" class="sociable-img sociable-hovers" title="Technorati" alt="Technorati" /></a><a rel="nofollow" target="_blank"  href="http://sphinn.com/index.php?c=post&amp;m=submit&amp;link=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/sphinn.png" class="sociable-img sociable-hovers" title="Sphinn" alt="Sphinn" /></a><a rel="nofollow" target="_blank"  href="http://www.mixx.com/submit?page_url=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731&amp;title=Output%20Popen%20subprocess%20stdout%20%28and%20stderr%29%20in%20real-time%20in%20Python" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/mixx.png" class="sociable-img sociable-hovers" title="Mixx" alt="Mixx" /></a><a rel="nofollow" target="_blank" title="Add to favorites" href="#" onclick="AddToFavorites(); return false;"><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/addtofavorites.png" class="sociable-img sociable-hovers" title="Add to favorites" alt="Add to favorites" /></a><a rel="nofollow" target="_blank"  href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731&amp;title=Output%20Popen%20subprocess%20stdout%20%28and%20stderr%29%20in%20real-time%20in%20Python" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/live.png" class="sociable-img sociable-hovers" title="Live" alt="Live" /></a><a rel="nofollow" target="_blank"  href="http://www.netvibes.com/share?title=Output%20Popen%20subprocess%20stdout%20%28and%20stderr%29%20in%20real-time%20in%20Python&amp;url=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/netvibes.png" class="sociable-img sociable-hovers" title="Netvibes" alt="Netvibes" /></a><a rel="nofollow" target="_blank"  href="http://www.scoopeo.com/scoop/new?newurl=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731&amp;title=Output%20Popen%20subprocess%20stdout%20%28and%20stderr%29%20in%20real-time%20in%20Python" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/scoopeo.png" class="sociable-img sociable-hovers" title="Scoopeo" alt="Scoopeo" /></a><a rel="nofollow" target="_blank"  href="http://www.viadeo.com/shareit/share/?url=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731&title=Output%20Popen%20subprocess%20stdout%20%28and%20stderr%29%20in%20real-time%20in%20Python&urllanguage=fr" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/viadeo.png" class="sociable-img sociable-hovers" title="viadeo FR" alt="viadeo FR" /></a><a rel="nofollow" target="_blank"  href="http://identi.ca/notice/new?status_textarea=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/identica.png" class="sociable-img sociable-hovers" title="Identi.ca" alt="Identi.ca" /></a><a rel="nofollow" target="_blank"  href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731&amp;t=Output%20Popen%20subprocess%20stdout%20%28and%20stderr%29%20in%20real-time%20in%20Python" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/myspace.png" class="sociable-img sociable-hovers" title="MySpace" alt="MySpace" /></a><a rel="nofollow" target="_blank"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731&amp;title=Output%20Popen%20subprocess%20stdout%20%28and%20stderr%29%20in%20real-time%20in%20Python" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/stumbleupon.png" class="sociable-img sociable-hovers" title="StumbleUpon" alt="StumbleUpon" /></a><a rel="nofollow" target="_blank"  href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Ftrollfactory.fr%2Foutput-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731&amp;submitHeadline=Output%20Popen%20subprocess%20stdout%20%28and%20stderr%29%20in%20real-time%20in%20Python&amp;submitSummary=So%20you%27re%20writing%20a%20Python%20script%20and%20you%20are%20launching%20some%20other%20external%20command.%0D%0A%0D%0ATo%20achieve%20that%2C%20you%20are%20using%20the%20subprocess.Popen%20command%2C%20right.%0D%0A%0D%0ABut%20the%20subprocess%20is%20very%20slow%2C%20and%20it%20outputs%20data%20during%20it%20runtime%2C%20so%20you%20would%20like%20t&amp;submitCategory=science&amp;submitAssetType=text" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/yahoobuzz.png" class="sociable-img sociable-hovers" title="Yahoo! Buzz" alt="Yahoo! Buzz" /></a><br/><br/>]]></content:encoded>
			<wfw:commentRss>http://trollfactory.fr/output-popen-subprocess-stdout-and-stderr-in-real-time-in-python-731/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
