<?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>Chris Haynie &#187; python</title>
	<atom:link href="http://chrishaynie.com/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://chrishaynie.com</link>
	<description>I&#039;ll fix it when its not broken</description>
	<lastBuildDate>Mon, 06 Sep 2010 08:06:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Python Threading</title>
		<link>http://chrishaynie.com/2009/06/python-threading/</link>
		<comments>http://chrishaynie.com/2009/06/python-threading/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 21:14:31 +0000</pubDate>
		<dc:creator>sax</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://chrishaynie.com/?p=94</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>In order to speed up some of my scripts I&#8217;ve implemented threading using python. Some scripts now run in 1/10th the time they used to as a result.  Here I will try to illustrate some of the key points, hopefully it will be useful to anyone.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">subprocess</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">threading</span> <span style="color: #ff7700;font-weight:bold;">import</span> Thread
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyThread<span style="color: black;">&#40;</span>Thread<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,server,<span style="color: #dc143c;">cmd</span><span style="color: black;">&#41;</span>:
        Thread.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">server</span> = server
        <span style="color: #008000;">self</span>.<span style="color: #dc143c;">cmd</span> = <span style="color: #dc143c;">cmd</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">status</span> = -<span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> run<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        proc = <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'ssh %s %s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">server</span>,<span style="color: #008000;">self</span>.<span style="color: #dc143c;">cmd</span><span style="color: black;">&#41;</span>,
                       shell=<span style="color: #008000;">True</span>,
                       stdin=<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">PIPE</span>,
                       stdout=<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">PIPE</span>,
                       stderr=<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">STDOUT</span>,
                       <span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">stdout</span>, <span style="color: #008000;">self</span>.<span style="color: black;">stderr</span> = proc.<span style="color: black;">communicate</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
servers = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'shell1.example.net'</span>,<span style="color: #483d8b;">'ssh2.mhost.com'</span>,<span style="color: #483d8b;">'examplebox.net'</span><span style="color: black;">&#93;</span>
command = <span style="color: #483d8b;">'uptime'</span>
cmdlist = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> srv <span style="color: #ff7700;font-weight:bold;">in</span> servers:
    run = MyThread<span style="color: black;">&#40;</span>srv,command<span style="color: black;">&#41;</span>
    cmdlist.<span style="color: black;">append</span><span style="color: black;">&#40;</span>run<span style="color: black;">&#41;</span>
    run.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> c <span style="color: #ff7700;font-weight:bold;">in</span> cmdlist:
    c.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;### %s:n%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>c.<span style="color: black;">server</span>,c.<span style="color: black;">stdout</span><span style="color: black;">&#41;</span>,
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;## Done&quot;</span></pre></div></div>

<p>In my example we loop through servers[] and for each server[] we run the specified command on them, in their own thread! 3 servers in the above example, not a big deal, what if you have 20 servers? 40 servers? huge speed improvement.</p>
<p>The script can be easily extended to say&#8230; read the servers list in from a configuration file, or accept the &#8216;command&#8217; from the command line arguments, (argv[1:]) etc.</p>
<p>Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://chrishaynie.com/2009/06/python-threading/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
