python多线程,队列按顺序运行,而不是并行运行

时间:2018-10-23 04:24:22

标签: python multithreading parallel-processing queue

为什么此代码不是并行运行,所以需要20秒才能运行,这意味着它是按顺序运行的。谢谢您的帮助。

import time
from queue import Queue
from threading import Thread
start = time.time()
def f():
    time.sleep(0.5)
    print("yes")
    return 'yes'


def do_stuff(q):
    while True:
        output = q.get()
        q.task_done()


q = Queue(maxsize=100)
for message_nbr in range(40):
    q.put(f())

num_threads = 10

for i in range(num_threads):
    worker = Thread(target=do_stuff, args=(q, ))
    worker.setDaemon(True)
    worker.start()

q.join()
print("time: ", time.time() - start)  # ~20 seconds

2 个答案:

答案 0 :(得分:0)

这是可行的!

request_threaded_irq()

答案 1 :(得分:0)

答案就在这里:

for message_nbr in range(40):
    q.put(f())

您正在将40个None实例放入队列,因为您正在调用f(),该实例返回None而不是传入f(函数对象)。此块占用20运行几秒钟!

更改此代码

def do_stuff(q):
    while True:
        output = q.get()
        q.task_done()

对此

def do_stuff(q):
    while True:
        output = q.get()
        output()
        q.task_done()

也是必需的(您需要调用该函数!)

最终:

import time
from queue import Queue
from threading import Thread
start = time.time()
def f():
    time.sleep(0.5)
    print("yes")
    return 'yes'


def do_stuff(q):
    while True:
        output = q.get()
        output()
        q.task_done()


q = Queue(maxsize=100)
for message_nbr in range(40):
    q.put(f)

num_threads = 10

for i in range(num_threads):
    worker = Thread(target=do_stuff, args=(q, ))
    worker.setDaemon(True)
    worker.start()

q.join()
print("time: ", time.time() - start)  # time:  2.183439254760742