在多執行緒程式的構建中容易犯乙個常見的錯誤:根據需要生成任意數目的執行緒,事實上,大多時候,程式將任務交給固定的,資料相對較少的一群執行緒(執行緒池)是較好的架構。該程式主要通過queue.queue
來實現執行緒池。
queue.queue
也是執行緒間通訊和同步的最有用最簡單的方式。
[root@xiaoxiong cb9]# cat cb2_9_4_sol_1.py
import threading, queue, time, sys
# globals (start with a capital letter)
qin = queue.queue()
qout = queue.queue()
qerr = queue.queue()
pool =
def report_error():
''' we "report" errors by adding error information to qerr '''
qerr.put(sys.exc_info()[:2])
def get_all_from_queue(q):
''' generator to yield one after the others all items currently
in the queue q, without any waiting
'''try:
while true:
yield q.get_nowait()
except queue.empty:
raise stopiteration
def do_work_from_queue():
''' the get-some-work, do-some-work main loop of worker threads '''
while true:
command, item = qin.get() # implicitly stops and waits
if command == 'stop':
break
try:
# simulated work functionality of a worker thread
if command == 'process':
result = 'new' + item
else:
raise valueerror, 'unknown command %r' % command
except:
# unconditional except is right, since we report _all_ errors
report_error()
else:
qout.put(result)
def make_and_start_thread_pool(number_of_threads_in_pool=5, daemons=true):
''' make a pool of n worker threads, daemonize, and start all of them '''
for i in range(number_of_threads_in_pool):
new_thread = threading.thread(target=do_work_from_queue)
new_thread.setdaemon(daemons)
new_thread.start()
def request_work(data, command='process'):
''' work requests are posted as (command, data) pairs to qin '''
qin.put((command, data))
def get_result():
return qout.get() # implicitly stops and waits
def show_all_results():
for result in get_all_from_queue(qout):
print 'result:', result
def show_all_errors():
for etyp, err in get_all_from_queue(qerr):
print 'error:', etyp, err
def stop_and_free_thread_pool():
# order is important: first, request all threads to stop...:
for i in range(len(pool)):
request_work(none, 'stop')
# ...then, wait for each of them to terminate:
for existing_thread in pool:
existing_thread.join()
# clean up the pool from now-unused thread objects
del pool[:]
for i in ('_ba', '_be', '_bo'): request_work(i)
make_and_start_thread_pool()
stop_and_free_thread_pool()
show_all_results()
show_all_errors()
[root@xiaoxiong cb9]#
[root@xiaoxiong cb9]#
[root@xiaoxiong cb9]#
[root@xiaoxiong cb9]#
[root@xiaoxiong cb9]#
[root@xiaoxiong cb9]#
[root@xiaoxiong cb9]# python cb2_9_4_sol_1.py
result: new_ba
result: new_be
result: new_bo
[root@xiaoxiong cb9]#
在物件導向程式設計中,建立和銷毀物件是很費時間的,因為建立乙個物件要獲取記憶體資源或者其它更多資源。所以提高服務程式效率的乙個
重要手段就是盡可能減少建立和銷毀物件的次數,特別是一些很耗資源的物件建立和銷毀。如何利用已有物件來服務就是乙個需要解決的關鍵問題,其實這就是一些"
池化資源
"技術產生的原因。本文介紹的執行緒池技術同樣符合這一思想。
python 執行緒池 Python的執行緒池
usr bin env python coding utf 8 concurrent 用於執行緒池和程序池程式設計而且更加容易,在python3.2中才有。import sys from concurrent.futures import threadpoolexecutor,as complete...
python 執行緒池 python執行緒池原始碼解析
本篇主要講下threadpoolexecutor的實現。由於業務量不大,且一直使用框架進行程式設計,對執行緒的理解一直很模糊,基本處於不想阻塞程式執行,起乙個執行緒啟動任務的階段。總感覺自己好像會執行緒一樣,實則一直處於一種懵懂狀態,通過一段時間檢視一些別人寫的原始碼,終於有所悟,也記錄下自己的學習...
python執行緒池
import time threadpool為執行緒池模組 import threadpool deftest str print str time.sleep 2 if name main starttime time.time 建立執行緒池,最多建立的執行緒數為10 pool threadpoo...