簡體   English   中英

如何多次並行運行單個 python function

[英]How to run a single python function in parallel multiple times

我有一個 python function 我想並行運行 100 次。 這樣做的代碼是什么

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


options = Options()
options.headless = True
options.add_experimental_option("excludeSwitches", ['enable-automation']);
options.add_argument('--user-agent="Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 640 XL LTE) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.10166"')
CHROMEDRIVER_PATH = 'c:\webdrivers\chromedriver.exe'
driver = webdriver.Chrome(CHROMEDRIVER_PATH, options=options)
def true():
    driver.get('https://youtu.be/Uhr5J5Sj-fU')
    driver.find_element_by_xpath('//*[@id="movie_player"]/div[2]/button/div').click()
    time.sleep(12*60+30)
    driver.quit()

使用 python 的線程模塊這可以幫助你

import threading
def hi():
    print('hi')
for i in range(0,100):
    t= threading.Thread(target=hi)
    t.start()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM