【问题标题】:How to run multiple Thread in Python如何在 Python 中运行多个线程
【发布时间】:2021-10-24 12:30:43
【问题描述】:

我必须运行所有 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)

【问题讨论】:

标签: python multithreading selenium webbrowser-control multiple-processes


【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-26
    • 2021-01-02
    • 2013-07-09
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多