<?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>Python Blog - All about python &#187; optimization</title>
	<atom:link href="http://www.python-blog.com/tag/optimization/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.python-blog.com</link>
	<description>and technologies around</description>
	<lastBuildDate>Tue, 27 Jul 2010 19:58:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Python threaded</title>
		<link>http://www.python-blog.com/2009/07/01/python-threaded/</link>
		<comments>http://www.python-blog.com/2009/07/01/python-threaded/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 21:37:19 +0000</pubDate>
		<dc:creator>Łukasz Balcerzak</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Threads]]></category>
		<category><![CDATA[IDE]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[port scan]]></category>
		<category><![CDATA[problem]]></category>
		<category><![CDATA[python blog]]></category>
		<category><![CDATA[socket]]></category>
		<category><![CDATA[SqlAlchemy]]></category>
		<category><![CDATA[thread queue]]></category>

		<guid isPermaLink="false">http://www.python-blog.com/?p=81</guid>
		<description><![CDATA[Recently I&#8217;ve run into some optimizations problem which I once hoped I wouldn&#8217;t had to face. After some research on the subject I decided to split some process using threads. I hadn&#8217;t have much experience in that area except some classes at PJIIT (Polish-Japanese Institute of Information Technology) so start was painful&#8230; but not for [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve run into some optimizations problem which I once hoped I wouldn&#8217;t had to face. After some research on the subject I decided to split some process using threads. I hadn&#8217;t have much experience in that area except some classes at PJIIT (Polish-Japanese Institute of Information Technology) so start was painful&#8230; but not for long (as with everything in Python).</p>
<p>So I decided to share my thoughts with everyone who would ever try to use threads in Python programming.</p>
<p>Let&#8217;s imagine simple case: you want to check some host if it has opened any port from range 1-1000. We create simple script (use whatever host you like)</p>
<pre class="brush:python">
import socket
import time

def is_port_open(host, port):
    """
    Takes host param as string and port param as int.
    Returns true if port is open and false otherwise.
    """
    #print "[DEBUG] Checking host %s:%s" % (host, port)
    s = socket.socket()
    is_open = True
    try:
        s.connect( (host, port) )
    except socket.error:
        is_open = False
    s.close()
    return is_open

def main():
    start = time.time()

    host = 'some.host'
    ports = range(1,1000) 

    print "[INFO] Will now check host %s for ports between %s and %s" % \
        (host, ports[0], ports[-1]) 

    for port in ports:
        if is_port_open(host, port):
            print "[INFO] Port %s on host %s is open." % (port, host)

    print "Elapsed Time: %s" % (time.time() - start)

if __name__ == '__main__':
    main()
</pre>
<p>How long was it? Well, try this then:</p>
<pre class="brush:python">
import threading
import socket
import time
import Queue

THREAD_NUMBER = 20

def is_port_open(host, port):
    """
    Takes host param as string and port param as int.
    Returns true if port is open and false otherwise.
    """
    #print "[DEBUG] Checking host %s:%s" % (host, port)
    s = socket.socket()
    is_open = True
    try:
        s.connect( (host, port) )
    except socket.error:
        is_open = False
    s.close()
    return is_open

class PortChecker(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue

    def run(self):
        while True:
            host, port = self.queue.get()
            if is_port_open(host, port):
                print "[INFO] Port %s on host %s is open." % (port, host)
            self.queue.task_done()

def main():
    start = time.time()

    host = 'some.host'
    ports = range(1,1000)
    queue = Queue.Queue()

    for i in xrange(THREAD_NUMBER):
        pc = PortChecker(queue)
        pc.setDaemon(True)
        pc.start()

    print "[INFO] Will now check host %s for ports between %s and %s" % \
        (host, ports[0], ports[-1]) 

    for port in ports:
        queue.put( (host, port) )

    queue.join()
    print "Elapsed Time: %s" % (time.time() - start)

if __name__ == '__main__':
    main()
</pre>
<p>Try with different port range/thread number. Ok, this is just an example. But it should give you an idea how to handle with some process that is done inside a loop. You have to define your &#8220;handler&#8221; (thread that will process on some object(s)), create empy queue and fill it with objects. Try to use only 1 thread (set THREAD_NUMBER to 1) and look at the time it took to finish whole process.</p>
<p>I know that programmer&#8217;s time is much more expensive than processor&#8217;s time and I try not to break this rule. But sometimes (i.e. when your script runs for over 12 hours) it is very nice if you can cut the time you can generate results. Just remember to catch exceptions inside threads &#8211; it is very important as if exception is risen by thread, it will just die without any notification.</p>
<p>Well, thats it. I will proceed tomorrow with some example with SQLAlchemy. Good night folks.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.python-blog.com/2009/07/01/python-threaded/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Python 3.1 released</title>
		<link>http://www.python-blog.com/2009/06/29/python-3-1-released/</link>
		<comments>http://www.python-blog.com/2009/06/29/python-3-1-released/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 15:12:40 +0000</pubDate>
		<dc:creator>Marcin Kuźmiński</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[Python 3.0]]></category>
		<category><![CDATA[Python 3.1]]></category>
		<category><![CDATA[python blog]]></category>

		<guid isPermaLink="false">http://www.python-blog.com/?p=76</guid>
		<description><![CDATA[Today a new version of Python came out Python 3.1. After having 2 RC &#8217;s final version is available for download here
Here&#8217;s what they say about this release at http://python.org site.
Python 3.1 is a continuation of the work started by Python 3.0, the new backwards-incompatible series of Python. Improvements in this release include:
* An ordered [...]]]></description>
			<content:encoded><![CDATA[<p>Today a new version of Python came out Python 3.1. After having 2 RC &#8217;s final version is available for download <strong><a href="http://python.org/download/releases/3.1/" target="_blank">here</a></strong><br />
Here&#8217;s what they say about this release at <strong><a href="http://python.org">http://python.org</a></strong> site.<br />
Python 3.1 is a continuation of the work started by Python 3.0, the new backwards-incompatible series of Python. Improvements in this release include:</p>
<p>* An ordered dictionary type<br />
* Various optimizations to the int type<br />
* New unittest features including test skipping and new assert methods.<br />
* A much faster io module<br />
* Tile support for Tkinter<br />
* A pure Python reference implementation of the import statement<br />
* New syntax for nested with statements</p>
<p>And the what&#8217;s new link <a href="http://docs.python.org/dev/py3k/whatsnew/3.1.html" target="_blank">http://docs.python.org/dev/py3k/whatsnew/3.1.html</a></p>
<p>I&#8217;ll for sure will give it a try soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.python-blog.com/2009/06/29/python-3-1-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
