0

次のコードを使用しています。これは、セレニウムを使用して10個のGoogle検索ページを開き、同時に10件の検索を行います。Pythonで並列実行が行われていることを確認してください

私はそれが正しく(平行に)実行されると思いますが、実際には一度にブラウザの5つのインスタンスを開くように見えるので、私は確信していません(ただし、これはgui opening )。

これは本当に並列に実行されている、または私もCPUにコアを使用する必要があるかどうかどうかを知るのが大好きだ(これは行うことができますか?)

test.py

import unittest 
from testsss import Testsss 
from concurrent.futures import ThreadPoolExecutor 

class Runner(): 

    def parallel_execution(self, *name): 

     suite = unittest.TestSuite() 

     for method in dir(Testsss): 
      if (method.startswith('test_selenium')): 
       print('testing') 
       suite.addTest(Testsss(method)) 

     with ThreadPoolExecutor(max_workers=10) as executor: 
      list_of_suites = list(suite) 
      for test in range(len(list_of_suites)): 
       test_name = str(list_of_suites[test]) 
       executor.submit(unittest.TextTestRunner().run, list_of_suites[test]) 

Runner().parallel_execution(Testsss) 

Testsss.py

from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import Select 
from selenium.common.exceptions import NoSuchElementException 
import unittest 
import time 

class Testsss(unittest.TestCase): 
    print('ok3') 
    def tearDown(self): 
     self.driver.quit() 
    @staticmethod  
    def selenium_test(self, testno): 
     print('ok4') 
     self.driver = webdriver.Firefox() 
     self.driver.get("http://google.com")  
     search_field = self.driver.find_element_by_id("lst-ib") 
     search_field.send_keys("Test " + str(testno)) 
     search_field.submit() 
     print("ok1") 

    def test_selenium_1(self): 
     Testsss.selenium_test(self,1) 

    def test_selenium_2(self): 
     Testsss.selenium_test(self, 2) 

    def test_selenium_3(self): 
     Testsss.selenium_test(self, 3) 

    def test_selenium_4(self): 
     Testsss.selenium_test(self, 4)   

    def test_selenium_5(self): 
     Testsss.selenium_test(self, 5)   

    def test_selenium_6(self): 
     Testsss.selenium_test(self, 6)   

    def test_selenium_7(self): 
     Testsss.selenium_test(self, 7)   

    def test_selenium_8(self): 
     Testsss.selenium_test(self, 8)   

    def test_selenium_9(self): 
     Testsss.selenium_test(self, 9)   

    def test_selenium_10(self): 
     Testsss.selenium_test(self, 10) 

答えて

1

あなたが実行中のプロセスを表示するtopシェルコマンドを使用することができますLinux上でこれを実行している場合。開いているときにShift-Hを押すと、スレッドが表示されます。 topを見ながらプログラムを実行すると、1つの親スレッドの下でいくつかのpythonスレッドが実行されているはずです。もしそうなら、それは働いている。

+0

ありがとうございました。私はこの方法でテストすることは考えていませんでした:) –

関連する問題