社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Mars

Mars 最近创建的主题
0
Redis计划建议
Redis Mars • 3 年前
Mars 最近回复了
6 年前
回复了 Mars 创建的主题 » 队列按顺序非并行运行的python多线程处理

答案就在这里:

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

您正在将40个None实例放入队列,因为您正在调用 f() 它不返回而不是传递 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