我第一次在生产代码观察这个问题,然后做了一个原型: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/)。 –
另请参阅多进程模块。 –