boh*_*nko 2 python multithreading
我首先在生产代码中观察到这个问题,然后做了一个原型:
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
Run Code Online (Sandbox Code Playgroud)
Heavyfunc() 的思想只是加载 CPU,没有任何同步和依赖。
使用1个线程时,平均需要4.14秒使用2个线程时,平均需要6.40秒不使用任何线程时,计算heavyfunc()平均需要2.07秒(多次测量,正好是4.14 / 2,如如果有 1 个线程和 2 个任务)。
如果有 2 个线程,我预计 2 个带有重函数() 的作业需要 2.07 秒。(我的 CPU 是 i7 => 有足够的内核)。
这是 CPU 监视器的屏幕截图,它也给出了没有真正多线程的想法:

我的想法错误在哪里?如何创建不干扰的 n 个线程?
CPython 不会一次在多个内核上执行字节码。多线程 cpu 绑定代码毫无意义。全局解释器锁 (GIL) 用于保护进程中的所有引用计数,因此一次只有一个线程可以使用 Python 对象。
您看到性能更差,因为您一次仍然只有一个线程在工作,但现在您也在更改线程上下文。