繁体   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