<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Multi-threading in Python</title>
	<atom:link href="http://www.artfulcode.net/articles/multi-threading-python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.artfulcode.net/articles/multi-threading-python/</link>
	<description>Resources and tips for dynamic, interactive languages.</description>
	<lastBuildDate>Mon, 23 Jan 2012 14:26:49 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Alfe</title>
		<link>http://www.artfulcode.net/articles/multi-threading-python/comment-page-1/#comment-3768</link>
		<dc:creator>Alfe</dc:creator>
		<pubDate>Mon, 23 Jan 2012 14:26:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/articles/multi-threading-python/#comment-3768</guid>
		<description>Hmpf.  Sorry for spamming :-(

The blog does not allow removing old posts and does not handle hard spaces properly at all places.  I hope you can decipher my code nevertheless.

Alfe</description>
		<content:encoded><![CDATA[<p>Hmpf.  Sorry for spamming :-(</p>
<p>The blog does not allow removing old posts and does not handle hard spaces properly at all places.  I hope you can decipher my code nevertheless.</p>
<p>Alfe</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alfe</title>
		<link>http://www.artfulcode.net/articles/multi-threading-python/comment-page-1/#comment-3767</link>
		<dc:creator>Alfe</dc:creator>
		<pubDate>Mon, 23 Jan 2012 14:24:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/articles/multi-threading-python/#comment-3767</guid>
		<description>I agree with Alastair.

class Worker(threading.Thread):
    def __init__(self, taskQueue, resultQueue):
        self.taskQueue = taskQueue
        self.resultQueue = resultQueue
        super(threading.Thread, self).__init__()
    def run(self):
        while True:  # until end marker found
            try:
                url = self.taskQueue().get(True)
                if url is None:  # end-marker?
                    self.resultQueue.put(self)  # report being finished
                f = urllib.urlopen(url)
                contents = f.read()
                f.close()
                self.resultQueue.put(contents)
            except Exception, e:
                self.resultQueue.put(e)

def get_files(urls):
    taskQueue = Queue()
    for url in urls:
        taskQueue.put(url)
    resultQueue = Queue()
    workers = set([ Worker(taskQueue, resultQueue) for i in range(4) ])
    for worker in workers:
        worker.start()
        taskQueue.put(None)  # one end marker for each worker thread
    while workers:
        result = resultQueue.get(True)
        if result in workers:
            workers.remove(result)
        else:
            yield result</description>
		<content:encoded><![CDATA[<p>I agree with Alastair.</p>
<p>class Worker(threading.Thread):<br />
    def __init__(self, taskQueue, resultQueue):<br />
        self.taskQueue = taskQueue<br />
        self.resultQueue = resultQueue<br />
        super(threading.Thread, self).__init__()<br />
    def run(self):<br />
        while True:  # until end marker found<br />
            try:<br />
                url = self.taskQueue().get(True)<br />
                if url is None:  # end-marker?<br />
                    self.resultQueue.put(self)  # report being finished<br />
                f = urllib.urlopen(url)<br />
                contents = f.read()<br />
                f.close()<br />
                self.resultQueue.put(contents)<br />
            except Exception, e:<br />
                self.resultQueue.put(e)</p>
<p>def get_files(urls):<br />
    taskQueue = Queue()<br />
    for url in urls:<br />
        taskQueue.put(url)<br />
    resultQueue = Queue()<br />
    workers = set([ Worker(taskQueue, resultQueue) for i in range(4) ])<br />
    for worker in workers:<br />
        worker.start()<br />
        taskQueue.put(None)  # one end marker for each worker thread<br />
    while workers:<br />
        result = resultQueue.get(True)<br />
        if result in workers:<br />
            workers.remove(result)<br />
        else:<br />
            yield result</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alfe</title>
		<link>http://www.artfulcode.net/articles/multi-threading-python/comment-page-1/#comment-3766</link>
		<dc:creator>Alfe</dc:creator>
		<pubDate>Mon, 23 Jan 2012 14:21:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/articles/multi-threading-python/#comment-3766</guid>
		<description>I agree with Alastair.

class Worker(threading.Thread):
    def __init__(self, taskQueue, resultQueue):
        self.taskQueue = taskQueue
        self.resultQueue = resultQueue
        super(threading.Thread, self).__init__()
    def run(self):
        while True:  # until end marker found
            try:
                url = self.taskQueue().get(True)
                if url is None:  # end-marker?
                    self.resultQueue.put(self)  # report being finished
                f = urllib.urlopen(url)
                contents = f.read()
                f.close()
                self.resultQueue.put(contents)
            except Exception, e:
                self.resultQueue.put(e)

def get_files(urls):
    taskQueue = Queue()
    for url in urls:
        taskQueue.put(url)
    resultQueue = Queue()
    workers = set([ Worker(taskQueue, resultQueue) for i in range(4) ])
    for worker in workers:
        worker.start()
        taskQueue.put(None)  # one end marker for each worker thread
    while workers:
        result = resultQueue.get(True)
        if result in workers:
            workers.remove(result)
        else:
            yield result</description>
		<content:encoded><![CDATA[<p>I agree with Alastair.</p>
<p>class Worker(threading.Thread):<br />
    def __init__(self, taskQueue, resultQueue):<br />
        self.taskQueue = taskQueue<br />
        self.resultQueue = resultQueue<br />
        super(threading.Thread, self).__init__()<br />
    def run(self):<br />
        while True:  # until end marker found<br />
            try:<br />
                url = self.taskQueue().get(True)<br />
                if url is None:  # end-marker?<br />
                    self.resultQueue.put(self)  # report being finished<br />
                f = urllib.urlopen(url)<br />
                contents = f.read()<br />
                f.close()<br />
                self.resultQueue.put(contents)<br />
            except Exception, e:<br />
                self.resultQueue.put(e)</p>
<p>def get_files(urls):<br />
    taskQueue = Queue()<br />
    for url in urls:<br />
        taskQueue.put(url)<br />
    resultQueue = Queue()<br />
    workers = set([ Worker(taskQueue, resultQueue) for i in range(4) ])<br />
    for worker in workers:<br />
        worker.start()<br />
        taskQueue.put(None)  # one end marker for each worker thread<br />
    while workers:<br />
        result = resultQueue.get(True)<br />
        if result in workers:<br />
            workers.remove(result)<br />
        else:<br />
            yield result</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Python Threading &#124; edgeArchitect</title>
		<link>http://www.artfulcode.net/articles/multi-threading-python/comment-page-1/#comment-3762</link>
		<dc:creator>Python Threading &#124; edgeArchitect</dc:creator>
		<pubDate>Sat, 14 Jan 2012 10:07:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/articles/multi-threading-python/#comment-3762</guid>
		<description>[...] Artful Code Youtube Video [...]</description>
		<content:encoded><![CDATA[<p>[...] Artful Code Youtube Video [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alastair</title>
		<link>http://www.artfulcode.net/articles/multi-threading-python/comment-page-1/#comment-3749</link>
		<dc:creator>Alastair</dc:creator>
		<pubDate>Wed, 21 Dec 2011 20:06:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/articles/multi-threading-python/#comment-3749</guid>
		<description>Perhaps I am missing something but why are you putting the threads themselves in a queue? This seems like a very strange use of queues. Also you end up creating one thread per task which is very wasteful.

Personally I would stick the jobs to be performed (in this case URLs to be downloaded) in a queue then create n (in this case 4) threads to work through that queue. If necessary, these threads can place the results in another queue to be processed by some other number of consumer threads.

This solution would have the huge advantage that jobs can be added as you go along, which is absolutely crucial if you want to chain a few of these things together.

I also do not understand the point of creating a Thread class to replace the get_files function. Ostensibly you do this to make things simpler, but actually I think it&#039;s more complicated (at least for illustration purposes). Potentially you might want your tasks to be objects so your pool of threads would take task objects from a queue, do something to those objects and put the &quot;finished&quot; objects into another queue for further processing. But I can&#039;t see why you need your tasks to be thread objects.</description>
		<content:encoded><![CDATA[<p>Perhaps I am missing something but why are you putting the threads themselves in a queue? This seems like a very strange use of queues. Also you end up creating one thread per task which is very wasteful.</p>
<p>Personally I would stick the jobs to be performed (in this case URLs to be downloaded) in a queue then create n (in this case 4) threads to work through that queue. If necessary, these threads can place the results in another queue to be processed by some other number of consumer threads.</p>
<p>This solution would have the huge advantage that jobs can be added as you go along, which is absolutely crucial if you want to chain a few of these things together.</p>
<p>I also do not understand the point of creating a Thread class to replace the get_files function. Ostensibly you do this to make things simpler, but actually I think it&#8217;s more complicated (at least for illustration purposes). Potentially you might want your tasks to be objects so your pool of threads would take task objects from a queue, do something to those objects and put the &#8220;finished&#8221; objects into another queue for further processing. But I can&#8217;t see why you need your tasks to be thread objects.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SrijonLab</title>
		<link>http://www.artfulcode.net/articles/multi-threading-python/comment-page-1/#comment-3748</link>
		<dc:creator>SrijonLab</dc:creator>
		<pubDate>Wed, 21 Dec 2011 07:48:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/articles/multi-threading-python/#comment-3748</guid>
		<description>cons_thread = threading.Thread( target=consumer, args=(q, len(files)) &quot;)&quot; is missing so put it</description>
		<content:encoded><![CDATA[<p>cons_thread = threading.Thread( target=consumer, args=(q, len(files)) &#8220;)&#8221; is missing so put it</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SrijonLab</title>
		<link>http://www.artfulcode.net/articles/multi-threading-python/comment-page-1/#comment-3747</link>
		<dc:creator>SrijonLab</dc:creator>
		<pubDate>Wed, 21 Dec 2011 07:47:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/articles/multi-threading-python/#comment-3747</guid>
		<description>and 
cons_thread = threading.Thread( target=consumer, args=(q, len(files)) &quot;)&quot; This is Missing on The Script So Put it</description>
		<content:encoded><![CDATA[<p>and<br />
cons_thread = threading.Thread( target=consumer, args=(q, len(files)) &#8220;)&#8221; This is Missing on The Script So Put it</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SrijonLab</title>
		<link>http://www.artfulcode.net/articles/multi-threading-python/comment-page-1/#comment-3746</link>
		<dc:creator>SrijonLab</dc:creator>
		<pubDate>Wed, 21 Dec 2011 07:45:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/articles/multi-threading-python/#comment-3746</guid>
		<description>You Have Error On Your Script...
Change  
&quot;for file in files:&quot; to 
&quot;for ufile in files:
   thread = FileGetter(ufile)&quot; 

and 

urllib.urlopen(self.url)</description>
		<content:encoded><![CDATA[<p>You Have Error On Your Script&#8230;<br />
Change<br />
&#8220;for file in files:&#8221; to<br />
&#8220;for ufile in files:<br />
   thread = FileGetter(ufile)&#8221; </p>
<p>and </p>
<p>urllib.urlopen(self.url)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://www.artfulcode.net/articles/multi-threading-python/comment-page-1/#comment-3719</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Wed, 09 Nov 2011 20:00:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/articles/multi-threading-python/#comment-3719</guid>
		<description>I get an error when running this that claims __init__ is never called. I believe you need         threading.Thread.__init__(self) to appear first in your def __init__ before anything else, otherwise self.url, etc. doesn&#039;t know what to reference.

Am I wrong?</description>
		<content:encoded><![CDATA[<p>I get an error when running this that claims __init__ is never called. I believe you need         threading.Thread.__init__(self) to appear first in your def __init__ before anything else, otherwise self.url, etc. doesn&#8217;t know what to reference.</p>
<p>Am I wrong?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: riz</title>
		<link>http://www.artfulcode.net/articles/multi-threading-python/comment-page-1/#comment-3248</link>
		<dc:creator>riz</dc:creator>
		<pubDate>Wed, 23 Mar 2011 04:51:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/articles/multi-threading-python/#comment-3248</guid>
		<description>This time I tried the application host to host, with TCP / IP connection using python, but sometimes during the same process occurs foult segmentation, it is when the query or insert into the table. whether the process should be the threading? and there are 2 processes in the same connection. A process runs every 5 minutes or so, and I make use of process B, but the response was too late and have been hit by a timeout, after 5 hours and then sent a late response but it did go through the process A. I try also to process B is not in time out but it is just as incurred through the process A. What should I do?

My email riz33a@yahoo.com

regard 
riz</description>
		<content:encoded><![CDATA[<p>This time I tried the application host to host, with TCP / IP connection using python, but sometimes during the same process occurs foult segmentation, it is when the query or insert into the table. whether the process should be the threading? and there are 2 processes in the same connection. A process runs every 5 minutes or so, and I make use of process B, but the response was too late and have been hit by a timeout, after 5 hours and then sent a late response but it did go through the process A. I try also to process B is not in time out but it is just as incurred through the process A. What should I do?</p>
<p>My email <a href="mailto:riz33a@yahoo.com">riz33a@yahoo.com</a></p>
<p>regard<br />
riz</p>
]]></content:encoded>
	</item>
</channel>
</rss>

