使用Future的Selenium python并发执行

时间:2018-12-27 04:39:45

标签: python multithreading selenium concurrency

我有一个用过的Python futures启动了硒自动化/抓取功能的多个实例。我的目标是使用多个浏览器实例并行运行同一功能,每个实例都有其自己的本地执行上下文。

我的代码的精简版是这样的:

from selenium import webdriver
from concurrent.futures import ThreadPoolExecutor

def utilityFunc():
  #async call to a captcha solving API
  return solvedCaptcha

class MyTableWidget(QWidget):

    def automate_process_1(threadNum):    
        driver = webdriver.Chrome()
        driver.get(url)

        #processing
        captcha = utilityFunc()
        print(f'{captcha}')

        #more processing

    def future_for_process_1(self):
        futures = []
        count = 4 #this number is obtained via user input

        with ThreadPoolExecutor(max_workers=count) as ex:
            i = 0
            while i < count:
                futures.append(ex.submit(self.automate_process,i))
                i = i+1

        for future in futures:
        future.result()

    # user clicks button to initiate futures creation
    self.button.clicked.connect(self.future_for_process_1)

我注意到有时print语句会为多个执行实例打印相同验证码值,这表明这种方法并不适用。

我希望为生成的每个执行实例提供一个完全本地执行上下文。有没有更好的方法可以做到这一点?

0 个答案:

没有答案