Я пытаюсь использовать concurrent.futures.ThreadPoolExecutor для параллельного запуска нескольких заданий. Это позволяет мне установить max_workers один раз, во время инициализации. Но по какой-то причине мое требование - менять количество активных потоков на лету. Я не мог найти способ сделать это. Попытка сброса "_max_workers" на лету, но это не оказало никакого влияния.
Вот код.
from concurrent.futures import ThreadPoolExecutor, as_completed
import urllib.request
from random import randint
from time import sleep
URLS = ['http://www.foxnews.com/',
'http://www.cnn.com/',
'http://europe.wsj.com/',
'http://www.bbc.co.uk/',
'http://some-made-up-domain.com/']
# Retrieve a single page and report the url and contents
def load_url(url, timeout):
with urllib.request.urlopen(url, timeout=timeout) as conn:
return conn.read()
if __name__ == "__main__":
pool = ThreadPoolExecutor(1)
fs = []
for url in URLS:
fs.append(pool.submit(load_url, url, 60))
r = randint(1,5)
print ("Changing max_workers to: {}".format(r))
pool._max_workers = r
print ("Current queue size: {}. Number of threads in parallel: {}".format(pool._work_queue.qsize(), len(pool._threads)))
for f in as_completed(fs):
r = randint(1,5)
print ("Changing max_workers to: {}".format(r))
pool._max_workers = r
print ("Current queue size: {}. Number of threads in parallel: {}".format(pool._work_queue.qsize(), len(pool._threads)))
try:
data = f.result()
except Exception as exc:
print('%r generated an exception: %s' % (url, exc))
else:
print('Page is %d bytes' % (len(data)))
print ("done")
Вот что он печатает:
Changing max_workers to: 3
Current queue size: 4. Number of threads in parallel: 1
Changing max_workers to: 1
Current queue size: 3. Number of threads in parallel: 1
Page is 233315 bytes
Changing max_workers to: 4
Current queue size: 2. Number of threads in parallel: 1
Page is 1127911 bytes
Changing max_workers to: 2
Current queue size: 1. Number of threads in parallel: 1
Page is 2741410 bytes
Changing max_workers to: 4
Current queue size: 0. Number of threads in parallel: 1
Page is 296361 bytes
done
Isесть ли способ изменить max_worker, который изменит активные потоки в pool._threads?