<?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>超群.com的博客 &#187; thread pool</title>
	<atom:link href="http://www.fuchaoqun.com/tag/thread-pool/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fuchaoqun.com</link>
	<description></description>
	<lastBuildDate>Thu, 08 Sep 2011 15:08:19 +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创建线程池</title>
		<link>http://www.fuchaoqun.com/2008/12/python-thread-pool/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=python-thread-pool</link>
		<comments>http://www.fuchaoqun.com/2008/12/python-thread-pool/#comments</comments>
		<pubDate>Tue, 23 Dec 2008 07:43:22 +0000</pubDate>
		<dc:creator>超群.com</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[thread pool]]></category>
		<guid isPermaLink="false">http://chaoqun.17348.com/?p=115</guid>
		<description><![CDATA[本博客所有原创文章采用知识共享署名-非商业性使用-相同方式共享，转载请保留链接http://chaoqun.17348.com/2008/12/python-thread-pool/ 最近在做一些数据挖掘方面的工作，需要对海量的数据进行处理，在目前的硬件环境下，多进程＋多线程的方式对运算时间的减少大有裨益，我用的是Python语言，开发效率高，运算效率也不低。 Python里面成型的线程池可以看以下http://www.chrisarndt.de/projects/threadpool/，看了一下API介绍，应该写的比较完备了，我这里想介绍的是Python线程池实现的原理以及一个简明的线程池代码实例。 我这里是用Queue这个包来实现的，Queue翻译成中文就是队列，全局的，我们把任务放进队列中去，然后开N个线程，每个线程都去队列中取一个任务，执行完了之后告诉系统说我执行完了，然后接着去队列中取下一个任务，直至队列中所有任务取空，退出线程。 这就是一般的线程池实现的原理，下面看一个实际的代码： import time import threading import Queue class Worker(threading.Thread):     def __init__(self, name, queue):         threading.Thread.__init__(self)         self.queue = queue         self.start()     def run(self): # 著名的死循环，保证接着跑下一个任务         while True:             # 队列为空则退出线程             if self.queue.empty():                 break             # 获取一个项目             foo = self.queue.get()             # 延时1S模拟你要做的事情             time.sleep(1)             [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>本博客所有原创文章采用<a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"><span style="color: #356aa0;">知识共享署名-非商业性使用-相同方式共享</span></a>，转载请保留链接<a href="http://chaoqun.17348.com/2008/12/python-thread-pool/">http://chaoqun.17348.com/2008/12/python-thread-pool/</a></p></blockquote>
<p>最近在做一些数据挖掘方面的工作，需要对海量的数据进行处理，在目前的硬件环境下，多进程＋多线程的方式对运算时间的减少大有裨益，我用的是Python语言，开发效率高，运算效率也不低。</p>
<p>Python里面成型的线程池可以看以下<a href="http://www.chrisarndt.de/projects/threadpool/" target="_blank">http://www.chrisarndt.de/projects/threadpool/</a>，看了一下API介绍，应该写的比较完备了，我这里想介绍的是Python线程池实现的原理以及一个简明的线程池代码实例。</p>
<p>我这里是用Queue这个包来实现的，Queue翻译成中文就是队列，全局的，<strong>我们把任务放进队列中去，然后开N个线程，每个线程都去队列中取一个任务，执行完了之后告诉系统说我执行完了，然后接着去队列中取下一个任务，直至队列中所有任务取空，退出线程。</strong></p>
<p>这就是一般的线程池实现的原理，下面看一个实际的代码：</p>
<blockquote>
<pre>import time
import threading
import Queue</pre>
<pre>class Worker(threading.Thread):
    def __init__(self, name, queue):
        threading.Thread.__init__(self)
        self.queue = queue
        self.start()</pre>
<pre>    def run(self):
        # 著名的死循环，保证接着跑下一个任务
        while True:
            # 队列为空则退出线程
            if self.queue.empty():
                break</pre>
<pre>            # 获取一个项目
            foo = self.queue.get()</pre>
<pre>            # 延时1S模拟你要做的事情
            time.sleep(1)</pre>
<pre>            # 打印
            print self.getName(),':', foo</pre>
<pre>            # 告诉系统说任务完成
            self.queue.task_done()</pre>
<pre># 队列
queue = Queue.Queue()</pre>
<pre># 加入100个任务队列
for i in range(100):
    queue.put(i)</pre>
<pre># 开10个线程
for i in range(10):
    threadName = 'Thread' + str(i)
    Worker(threadName, queue)</pre>
<pre># 所有线程执行完毕后关闭
queue.join()</pre>
</blockquote>
<p>那些成型的代码都非常的复杂，想了解实现原理难度颇大，希望这篇短文能起到拨开云雾的功用。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fuchaoqun.com/2008/12/python-thread-pool/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
