ThreadPoolExecutor 不会在最后一个线程完成之前启动一个新线程

Doesn't ThreadPoolExecutor start a new thread before the last thread is finished

我已经在 Colab 上写下了这个

import threading
import time
import os

class thread():
    def __init__(self, url, id, sleep_time):
        self.url = url
        self.id = id
        self.sleep_time = sleep_time

    def run(self, key):
        print("Task Executed {}".format(threading.current_thread()))
        self.upload_image(key)

    def upload_image(self, key):
        for i in range(10):
          print(self.id, "->", key)
          print()
          time.sleep(10)

with ThreadPoolExecutor(max_workers=1000) as executor:
  for i in range(1000):
    t = thread("url", i, 0.5)
    print("start the id: ", i)
    executor.submit(t.run("gg"))

每当最后一个线程是 运行 时,它将在执行下一次打印之前休眠 10 秒。但是,当前一个线程 运行ning 时,程序不会启动新线程。比如线程0是运行ning,它休眠了10秒,程序执行线程0的时候不是应该启动线程1吗?

我认为问题在于下面的代码行

executor.submit(t.run("gg"))

参考:https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Executor.submit 关于如何将参数传递给 then 函数 运行

你没有告诉执行者运行这个方法,你自己在执行。

executor.submit(t.run("gg"))

这将评估 t.run("gg") - 即在提交之前执行函数。

你需要告诉执行者要执行什么,但不执行:

executor.submit(t.run, ("gg",))

这里我告诉它用参数 ("gg",)

t.run

这导致:

start the id:  0
Task Executed <Thread(ThreadPoolExecutor-0_0, started 123145503694848)>
0 -> ('gg',)

start the id:  1
Task Executed <Thread(ThreadPoolExecutor-0_1, started 123145520484352)>
1 -> ('gg',)
...