# -*- coding: utf-8 -*-
import urllib2
import time
import socket
from datetime import datetime
from thread_pool import *
import queue, threading, sys
from threading import thread
import time
import urllib
# working thread
class worker(thread):
worker_count = 0
timeout = 1
def __init__( self, workqueue, resultqueue, **kwds):
thread.__init__( self, **kwds )
self.id = worker.worker_count
worker.worker_count += 1
self.setdaemon( true )
self.workqueue = workqueue
self.resultqueue = resultqueue
self.start( )
def run( self ):
''''''' the get-some-work, do-some-work main loop of worker threads '''
#死迴圈,從而讓建立的執行緒在一定條件下關閉退出
while true:
try:
callable, args, kwds = self.workqueue.get(timeout=worker.timeout)
res = callable(*args, **kwds)
#print "worker[%2d]: %s" % (self.id, str(res) )
self.resultqueue.put( res )
#time.sleep(worker.sleep)
except queue.empty:
break
except :
print 'worker[%2d]' % self.id, sys.exc_info()[:2]
raise
class workermanager(object):
def __init__( self, num_of_workers=10, timeout = 2):
self.workqueue = queue.queue()
self.resultqueue = queue.queue()
self.workers =
self.timeout = timeout
self._recruitthreads( num_of_workers )
"""
初始化執行緒
"""
def _recruitthreads( self, num_of_workers ):
for i in range( num_of_workers ):
worker = worker( self.workqueue, self.resultqueue )
"""
等待所有執行緒執行完畢
"""
def wait_for_complete( self):
# ...then, wait for each of them to terminate:
while len(self.workers):
worker = self.workers.pop()
worker.join( )
if worker.isalive() and not self.workqueue.empty():
print "all jobs are are completed."
"""
新增一項工作入隊
"""
def add_job( self, callable, *args, **kwds ):
self.workqueue.put( (callable, args, kwds) )
def get_result( self, *args, **kwds ):
return self.resultqueue.get( *args, **kwds )
def main():
#使用執行緒池
socket.setdefaulttimeout(10)
#print 'start testing'
start = time.time()
wm = workermanager(100)
for i in range(0,1000):
wm.add_job(do_get_con,i)
wm.wait_for_complete()
end = time.time()
print 'end testing'
print "cost all time: %s" % (end-start)
def do_get_con(args):
try:
time.sleep(1)#模擬處理時間
#print args
#print '#'
print threading.current_thread(), list(args)
except exception,e:
pass
if __name__ == "__main__":
main()
簡單執行緒池類
簡單練習了一下 簡單實現了一下執行緒池類,增加對執行緒的理解和掌控。以後有時間再好好完善下,現在和大家分享下 include include include include include include include include include include include include...
簡單執行緒池實現
執行緒池可以處理多執行緒問題,只要將任務放到任務佇列中,執行緒池中的執行緒就會從佇列中取任務,以預設的優先順序開始執行,如果你的任務數大於正在工作的執行緒數,則執行緒池將會建立一根新的執行緒來輔助工作,但是永遠都不會超過執行緒池中線程的最大值。執行緒池的結構 pragma once include ...
簡單執行緒池實現
1.用於執行大量相對短暫的任務 2.當任務增加的時候能夠動態的增加執行緒池中線程的數量值到達乙個閾值 3.當任務執行完畢的時候,能夠動態的銷毀執行緒池中的執行緒 4.該執行緒池的實現本質上也是生產者與消費者模型的應用。生產者執行緒向任務佇列新增任務,一旦佇列有任務到來,如果有等待 執行緒就喚醒來執行...