我第一次在生產代碼觀察這個問題,然後做了一個原型:Python的多線程代碼是慢於單線程
import threading, Queue, time, sys
def heavyfunc():
''' The idea is just to load CPU '''
sm = 0
for i in range(5000):
for j in range(5000):
if i + j % 2 == 0:
sm += i - j
print "sm = %d" % sm
def worker(queue):
''' worker thread '''
while True:
elem = queue.get()
if elem == None: break
heavyfunc() # whatever the elem is
starttime = time.time()
q = Queue.Queue() # queue with tasks
number_of_threads = 1
# create & start number_of_threads working threads
threads = [threading.Thread(target=worker, args=[q]) for thread_idx in range(number_of_threads)]
for t in threads: t.start()
# add 2 working items: they are estimated to be computed in parallel
for x in range(2):
q.put(1)
for t in threads: q.put(None) # Add 2 'None' => each worker will exit when gets them
for t in threads: t.join() # Wait for every worker
#heavyfunc()
elapsed = time.time() - starttime
print >> sys.stderr, elapsed
heavyfunc的想法()僅僅是加載CPU,沒有任何同步和依賴。
當使用1個線程,需要4.14秒中平均 當使用2個線程,它在不使用任何線程,以計算heavyfunc()採用6.40秒中平均 取2.07秒的平均(測定多次,這是正好是4.14/2,就像1個線程和2個任務一樣)。
我在等2個線程期望2個帶有heavyfunc()的作業需要2.07秒。 (我的CPU是i7 =>有足夠的核心)。
這裏是CPU顯示器的屏幕截圖也給有沒有真正的多線程的想法:
哪裏是在我的思想的錯誤?如何創建不干擾的n個線程?
正如在線程模塊的文檔開始的相當大的筆記所說.. CPython中只有一個線程,使用進程。 – Voo
這是衆所周知的Python行爲,請參閱[瞭解Python GIL](http://www.dabeaz.com/GIL/)。 –
另請參閱多進程模塊。 –