简体   繁体   中英

How to run a single python function in parallel multiple times

I have a python function which I want to run 100 times in parallel. What is the code to do that

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()

Use threading module of python This may help you

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM