python2.7线程池比一个一个启动线程要快很多

python2.7 threading pool is much faster than starting the thread one by one

我在线程池上做了一些测试,并一个一个地启动线程。使用线程池时要快得多。我知道为什么了吗?

import threading
import Queue
import time

def stringFunction(value, out_queue):
    my_str = "This is string no. " + value
    time.sleep(5)
    out_queue.put(my_str)

my_queue = Queue.Queue()
i  = 1
threads = []
while i<10:
    thread = threading.Thread(stringFunction(str(i), my_queue))
    thread.start()
    threads.append(thread)
    i = i + 1

for thread in threads:
    thread.join()


func_value = list(my_queue.queue)
print func_value

[运行] python -u "c:\Users\eguozqu\git\test\thread_test.py" ['This is string no. 1', 'This is string no. 2', 'This is string no. 3', 'This is string no. 4', 'This is string no. 5', 'This is string no. 6', 'This is string no. 7', 'This is string no. 8', 'This is string no. 9']

[完成] 在 45.353 秒内以代码=0 退出

from multiprocessing.pool import ThreadPool as Pool
import time
class someClass(object):
   def __init__(self):
       pass
   def f(self, x):
       time.sleep(5)
       return x*x

   def go(self):
      p = Pool(10)
      sc = p.map(self, range(10))
      print sc

   def __call__(self, x):   
     return self.f(x)

sc = someClass()
sc.go()

[运行] python -u "c:\Users\eguozqu\git\test\process_test.py" [0、1、4、9、16、25、36、49、64、81]

[完成] 在 5.393 秒内以代码=0 退出

当您执行 threading.Thread(stringFunction(str(i), my_queue)) 时,您正在调用 stringFunction 并将结果传递给 threading.Thread 的初始化程序,而不是传递函数本身。

这意味着每次创建线程时,您首先要等待该函数调用完成。

您可能想要的是 threading.Thread(target=stringFunction, args=(str(i), my_queue))