2006年09月09日 星期六 08:35
¶ÞiÛ
2006年09月09日 星期六 10:20
On 9/9/06, 谢小漫 <cat在ewyu.com> wrote: > > 线程是这个: > threads=[] > nloops = range(10) > for i in nloops: > t = threading.Thread(target=vote) > threads.append(t) > for i in nloops: #start threads > threads[i].start() > for i in nloops: #wait for all threads to finish > threads[i].join() > > 我想问问:如何用十个线程来完成100个任务。 > 就是问问如何做线程数量控制。 > 再用一个线程负责线程的调度。 -- I like python! My Blog: http://www.donews.net/limodou UliPad Site: http://wiki.woodpecker.org.cn/moin/UliPad UliPad Maillist: http://groups.google.com/group/ulipad
2006年09月09日 星期六 13:05
©6¬¹ç^~Øky§_ºw0)^N»µ«$ª)µ«$iËb¢}:÷×µì2¹Ê&¶ÞiÛ
2006年09月09日 星期六 14:27
线程池 是用来控制线程线程数量的, 适用于 任务 执行的时间比较短, 但是我不知道 , python 有没有 提供 线程池 这个东西. 但是在 cherrypy 里有一个 设置 线程池 线程数的 选项 On 9/9/06, bird devdoer <devdoer at gmail.com> wrote: > > 设置一个任务队列即可. > q=TaskQueue() > def thread_func(): > whileTrue: > task=q.pop() > task.action() > > > > > 在06-9-9,谢小漫 <cat at ewyu.com> 写道: > > > > > 线程是这个: > threads=[] > nloops = range(10) > for i in nloops: > t = threading.Thread(target=vote) > threads.append(t) > for i in nloops: #start threads > threads[i].start() > for i in nloops: #wait for all threads to finish > threads[i].join() > > 我想问问:如何用十个线程来完成100个任务。 > 就是问问如何做线程数量控制。 > > -- > 花开邑大,漫步心月湖。 > http://www.ewyu.com/ > _______________________________________________ > > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to > python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request at lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > > > > > -- > devdoer > devdoer at gmail.com > http://project.mytianwang.cn/cgi-bin/blog > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to > python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request at lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese >
2006年09月09日 星期六 14:31
线程池 也可以 自己来实现, 以前 看过 一个 linux 下实现 线程池的 文章 其实就是 托管的 线程的创建, 给当前的线程计数, 如果 达到了做大 限制, 那么创建 线程的动作将 阻塞, 等待 有线程退出. On 9/9/06, lu <lubiao.py at gmail.com> wrote: > 线程池 是用来控制线程线程数量的, > > 适用于 任务 执行的时间比较短, > > 但是我不知道 , python 有没有 提供 线程池 这个东西. > 但是在 cherrypy 里有一个 设置 线程池 线程数的 选项 >
2006年09月11日 星期一 01:37
这个是我以前写的一个简陋的线程池(忘了参考那本书的例子了,大概是cookbook). 你大可参考一下.
import Queue, threading,sys
from threading import Thread
# working thread
class Peon( Thread ):
def __init__( self, id, workQueue):
Thread.__init__( self, target=self.work )
self.id=id
self.workQueue=workQueue
def work( self ):
''' the get-some-work, do-some-work main loop of worker threads '''
while True:
command, job = self.workQueue.get() # implicitly
stops and waits
if command == 'stop':
print 'Peon '+str( self.id )+' is killed!'
break
try:
# simulated work functionality of a worker thread
if command == 'process':
print 'Peon %d is working on %s'%(self.id,job)
# do the job here.
else:
raise ValueError, 'Unknown command %r' % command
except:
# unconditional except is right, since we report _all_ errors
print sys.exc_info()[:2]
# This is the tread pool
class GreatHall:
command_run='process'
command_stop='stop'
def __init__( self, num_of_peons=5 ):
self.workQueue=Queue.Queue()
self.num_of_peons=num_of_peons
self.pool=[]
self.recruitPeons( num_of_peons )
def recruitPeons( self, num_of_peons=5 ):
#init the thread pool
for i in range( 0, num_of_peons ):
new_peon=Peon( i, self.workQueue)
new_peon.setDaemon( True )
new_peon.start()
self.pool.append(new_peon)
print 'Peon '+str( i )+' is ready!'
def sendCommand( self, data, command='process' ):
self.workQueue.put( ( command, data ) )
def wait_for_stop( self ):
# order is important: first, request all threads to stop...:
for i in range( len( self.pool ) ):
self.sendCommand( None, 'stop' )
# ...then, wait for each of them to terminate:
for existing_peon in self.pool:
existing_peon.join()
# clean up the pool
del self.pool[:]
great_hall=GreatHall()
for i in range(100):
great_hall.sendCommand(str(i))
great_hall.wait_for_stop()
On 9/9/06, lu <lubiao.py在gmail.com> wrote:
> 线程池 也可以 自己来实现,
> 以前 看过 一个 linux 下实现 线程池的 文章
>
> 其实就是 托管的 线程的创建, 给当前的线程计数, 如果 达到了做大 限制, 那么创建 线程的动作将
> 阻塞, 等待 有线程退出.
>
> On 9/9/06, lu <lubiao.py在gmail.com> wrote:
> > 线程池 是用来控制线程线程数量的,
> >
> > 适用于 任务 执行的时间比较短,
> >
> > 但是我不知道 , python 有没有 提供 线程池 这个东西.
> > 但是在 cherrypy 里有一个 设置 线程池 线程数的 选项
> >
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
2006年09月11日 星期一 11:43
ÛM:ÿßõÕ.â½ë!yê`¨¥r¢¢"+µ¹ëaæx,ÊÇë¢kaæx"+µ8ky§p¢¹"aærV¬°÷¨8ky§]yø§ÛøÂäBç®y8ky§b+lzWíj¸¶Ç¥ +Ç¥~'
Zeuux © 2025
京ICP备05028076号