<?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; Threads</title>
	<atom:link href="http://www.python-blog.com/category/threads/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>hg app &#8211; mercurial hgwebdir replacement.</title>
		<link>http://www.python-blog.com/2010/06/01/hg-app-mercurial-hgwebdir-replacement/</link>
		<comments>http://www.python-blog.com/2010/06/01/hg-app-mercurial-hgwebdir-replacement/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 18:41:44 +0000</pubDate>
		<dc:creator>Marcin Kuźmiński</dc:creator>
				<category><![CDATA[Mercurial]]></category>
		<category><![CDATA[Pylons]]></category>
		<category><![CDATA[SqlAlchemy]]></category>
		<category><![CDATA[Threads]]></category>
		<category><![CDATA[hgweb alternative]]></category>
		<category><![CDATA[hgweb templates]]></category>
		<category><![CDATA[hgwebdir replacement]]></category>
		<category><![CDATA[mercurial web]]></category>
		<category><![CDATA[pylons mercurial]]></category>
		<category><![CDATA[vcs]]></category>

		<guid isPermaLink="false">http://www.python-blog.com/?p=297</guid>
		<description><![CDATA[Since, me and Lukasz fine tuned the vcs library i manage to setup a beta version of hg app. I was tired of the boring looks, and poor source code browsing capabilities of the hgwebdir.
So i started to write my own app, at the beginning it was just a simple hgwebdir wrapper, to pylons, now [...]]]></description>
			<content:encoded><![CDATA[<p>Since, me and Lukasz fine tuned the <a href="http://bitbucket.org/marcinkuzminski/vcs">vcs</a> library i manage to setup a beta version of <a href="http://bitbucket.org/marcinkuzminski/hg-app/">hg app</a>. I was tired of the boring looks, and poor source code browsing capabilities of the hgwebdir.<br />
So i started to write my own app, at the beginning it was just a simple hgwebdir wrapper, to pylons, now it&#8217;s fully standalone replacement. Including abilities to push/pull thrum the application.</p>
<p>Pylons based replacement for hgwebdir. Fully customizable,<br />
with authentication, permissions. Based on vcs library.</p>
<ul>
<li> has it&#8217;s own middleware to handle mercurial protocol request each request can<br />
be logged and authenticated +threaded performance unlikely to hgweb. Middleware recognises a mercurial protocol<br />
and skips any functionality for pure web request</li>
<li>mimics full functionality of hgwebdir including feeds,archives,diffs,raw-diffs,annotations,file history etc.</li>
<li> mako templates let&#8217;s you customize look and feel of the application.<br />
Possibly visual merge with your company systems</li>
<li>diffs annotations and source code all colored by pygments (see screenshots).</li>
<li> admin interface for performing user/permission managements as well as repository<br />
managements. Out of beta version should have fully customizable permission system, with<br />
private repos, and repo permissions</li>
<li> added cache with invalidation on push/repo managment for high performance and<br />
always upto date data.</li>
<li> rss / atom feeds</li>
<li> future support for git (thanks to vcs mercurial/git api should be 1:1</li>
<li> based on pylons 1.0 / sqlalchemy 0.6 /sqlite(possibly mysql/postgres thanks to sqlalchemy models)</li>
</ul>
<p>Below are the beta version screenshots. Now I&#8217;m not good at designing webapps, so excuse me if you feel the look&amp;feel to be to simple or bad looking.<br />
It&#8217;s all i could come up with. Most of my expiration was taken from hgwebdir/bitbucket and github. I tried to take all best parts from all of them and combine into this app.<br />
It&#8217;s still in beta but i think it&#8217;s production ready in this stage I&#8217;m using it for few weeks, and had no troubles or crashes with it. The code can be grabbed at bitbucket repo <a href="http://bitbucket.org/marcinkuzminski/hg-app/">http://bitbucket.org/marcinkuzminski/hg-app/</a></p>

<a href='http://www.python-blog.com/2010/06/01/hg-app-mercurial-hgwebdir-replacement/screenshot-administration-namoroka/' title='Screenshot-Administration - Namoroka'><img width="150" height="150" src="http://www.python-blog.com/wp-content/uploads/2010/06/Screenshot-Administration-Namoroka-150x150.png" class="attachment-thumbnail" alt="" title="Screenshot-Administration - Namoroka" /></a>
<a href='http://www.python-blog.com/2010/06/01/hg-app-mercurial-hgwebdir-replacement/screenshot-file-annotate-namoroka/' title='Screenshot-File annotate - Namoroka'><img width="150" height="150" src="http://www.python-blog.com/wp-content/uploads/2010/06/Screenshot-File-annotate-Namoroka-150x150.png" class="attachment-thumbnail" alt="" title="Screenshot-File annotate - Namoroka" /></a>
<a href='http://www.python-blog.com/2010/06/01/hg-app-mercurial-hgwebdir-replacement/screenshot-python-works-mercurial-repositories-namoroka/' title='Screenshot-Python-works Mercurial Repositories - Namoroka'><img width="150" height="150" src="http://www.python-blog.com/wp-content/uploads/2010/06/Screenshot-Python-works-Mercurial-Repositories-Namoroka-150x150.png" class="attachment-thumbnail" alt="" title="Screenshot-Python-works Mercurial Repositories - Namoroka" /></a>
<a href='http://www.python-blog.com/2010/06/01/hg-app-mercurial-hgwebdir-replacement/screenshot-python-works-mercurial-repositories-namoroka-1/' title='Screenshot-Python-works Mercurial Repositories - Namoroka-1'><img width="150" height="150" src="http://www.python-blog.com/wp-content/uploads/2010/06/Screenshot-Python-works-Mercurial-Repositories-Namoroka-1-150x150.png" class="attachment-thumbnail" alt="" title="Screenshot-Python-works Mercurial Repositories - Namoroka-1" /></a>
<a href='http://www.python-blog.com/2010/06/01/hg-app-mercurial-hgwebdir-replacement/screenshot-repository-managment-namoroka/' title='Screenshot-Repository managment - Namoroka'><img width="150" height="150" src="http://www.python-blog.com/wp-content/uploads/2010/06/Screenshot-Repository-managment-Namoroka-150x150.png" class="attachment-thumbnail" alt="" title="Screenshot-Repository managment - Namoroka" /></a>
<a href='http://www.python-blog.com/2010/06/01/hg-app-mercurial-hgwebdir-replacement/screenshot-repository-managment-namoroka-1/' title='Screenshot-Repository managment - Namoroka-1'><img width="150" height="150" src="http://www.python-blog.com/wp-content/uploads/2010/06/Screenshot-Repository-managment-Namoroka-1-150x150.png" class="attachment-thumbnail" alt="" title="Screenshot-Repository managment - Namoroka-1" /></a>
<a href='http://www.python-blog.com/2010/06/01/hg-app-mercurial-hgwebdir-replacement/screenshot-repository-managment-namoroka-2/' title='Screenshot-Repository managment - Namoroka-2'><img width="150" height="150" src="http://www.python-blog.com/wp-content/uploads/2010/06/Screenshot-Repository-managment-Namoroka-2-150x150.png" class="attachment-thumbnail" alt="" title="Screenshot-Repository managment - Namoroka-2" /></a>

]]></content:encoded>
			<wfw:commentRss>http://www.python-blog.com/2010/06/01/hg-app-mercurial-hgwebdir-replacement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>multiprocessing get number of cpu&#8217;s</title>
		<link>http://www.python-blog.com/2009/07/13/multiprocessing-get-number-of-cpus/</link>
		<comments>http://www.python-blog.com/2009/07/13/multiprocessing-get-number-of-cpus/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 22:04:13 +0000</pubDate>
		<dc:creator>Marcin Kuźmiński</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Threads]]></category>
		<category><![CDATA[cores]]></category>
		<category><![CDATA[Multiprocessing]]></category>
		<category><![CDATA[number of cores]]></category>
		<category><![CDATA[number of processors]]></category>
		<category><![CDATA[python blog]]></category>
		<category><![CDATA[python multiprocessing]]></category>

		<guid isPermaLink="false">http://www.python-blog.com/?p=120</guid>
		<description><![CDATA[I always wanted to know if there a way to find out how many cpu&#8217;s does the machine have. For example to run n number of processes based on number of cores, recently i was exploring multiprocessing module and i found that there is a method that show  up number of cpu&#8217;s / cores that [...]]]></description>
			<content:encoded><![CDATA[<p>I always wanted to know if there a way to find out how many cpu&#8217;s does the machine have. For example to run n number of processes based on number of cores, recently i was exploring multiprocessing module and i found that there is a method that show  up number of cpu&#8217;s / cores that the machine have.</p>
<pre class="brush:python">
import multiprocessing

multiprocessing.cpu_count()
# this shows up 2 for my core2duo e9300

#so now if we iterate this like that

for i in xrange(multiprocessing.cpu_count()):
    print 'core',i+1

#this prints
#core 1
#core 2
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.python-blog.com/2009/07/13/multiprocessing-get-number-of-cpus/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python threads tutorial</title>
		<link>http://www.python-blog.com/2009/07/07/python-threads-tutorial/</link>
		<comments>http://www.python-blog.com/2009/07/07/python-threads-tutorial/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 08:29:59 +0000</pubDate>
		<dc:creator>Marcin Kuźmiński</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Threads]]></category>
		<category><![CDATA[GIL]]></category>
		<category><![CDATA[learning python]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[python blog]]></category>
		<category><![CDATA[thread queue]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.python-blog.com/?p=113</guid>
		<description><![CDATA[If you&#8217;re looking how to start with threads in python i found a great pdf to start with.
Download the Python-Threads pdf. Whitch includes the basic, and some more advanced information and tutorials about the threads in python.
In the pdf you can useful find info about:

thread and threading modules
queue
locks
gil
events
debugging threads

]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re looking how to start with threads in python i found a great pdf to start with.</p>
<p>Download the <strong><a href="http://www.python-blog.com/wp-content/uploads/2009/07/Python-Threads.pdf">Python-Threads</a></strong> pdf. Whitch includes the basic, and some more advanced information and tutorials about the threads in python.</p>
<p>In the pdf you can useful find info about:</p>
<ul>
<li>thread and threading modules</li>
<li>queue</li>
<li>locks</li>
<li>gil</li>
<li>events</li>
<li>debugging threads</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.python-blog.com/2009/07/07/python-threads-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
	</channel>
</rss>
