1

我必须运行所有 50 个 chrome。我想总是同时运行 5 个 chrome。如果 1 in 5 chrome 先关闭,它将运行 1 chrome new。直到 50 镀铬完成。

示例:5 chrome 正在运行。如果 chrome 3 先关闭,chrome 6 将运行。所以它总是同时运行 5 个 chrome。

我该怎么做?

run_chrome = 50
thread = 5
def run_thread(thread_amount):
    my_thread = []
    for i in range(thread_amount):
        my_thread.append(threading.Thread(target=run_chrome_browser, args=[]))
    for t in my_thread:
        t.start()
    for t in my_thread:
        t.join()
while thread < run_chrome:
    run_thread(thread)
    run_chrome = run_chrome - thread
run_thread(run_chrome)
4

1 回答 1

1

线程池是解决方案之一。

import time

from concurrent.futures import ThreadPoolExecutor

pool = ThreadPoolExecutor(max_workers=5)

def chrome_app_func(i):
    print(f"Fake chrome app: {i}")
    time.sleep(10)
    print(f"Fake chrome app: {i} -- finished")


for i in range(50):
    pool.submit(chrome_app_func, i)
于 2021-08-24T09:25:02.387 回答