<?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>Sun, 29 Jan 2012 18:46:00 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.2.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>Chris Haynie</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><pre lang="python">
#!/usr/bin/python
import sys
import subprocess
from threading import Thread

class MyThread(Thread):
    def __init__(self,server,cmd):
        Thread.__init__(self)
        self.server = server
        self.cmd = cmd
        self.status = -1
    def run(self):
        proc = subprocess.Popen('ssh %s %s' % (self.server,self.cmd),
                       shell=True,
                       stdin=subprocess.PIPE,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.STDOUT,
                       )
        self.stdout, self.stderr = proc.communicate()

servers = ['shell1.example.net','ssh2.mhost.com','examplebox.net']
command = 'uptime'
cmdlist = []
for srv in servers:
    run = MyThread(srv,command)
    cmdlist.append(run)
    run.start()

for c in cmdlist:
    c.join()
    print "### %s:n%s" % (c.server,c.stdout),
print "## Done"
</pre><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>
<!-- Served from: chrishaynie.com @ 2012-02-06 23:14:54 by W3 Total Cache -->
