【发布时间】:2021-03-01 08:14:03
【问题描述】:
我已经在 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 秒,然后再执行下一次打印。但是,当前一个线程正在运行时,该程序不会启动新线程。比如线程 0 正在运行,并且休眠了 10 秒,那么程序在执行线程 0 时不应该启动线程 1 吗?
【问题讨论】:
标签: python multithreading threadpoolexecutor