检查Python中是否发生了并行执行

时间:2017-03-11 13:22:25

标签: python parallel-processing python-unittest concurrent.futures

我有以下代码,它使用selenium打开10个Google搜索页面并同时搜索所有10个搜索。

我认为它运行正常(并行),但我不确定,因为它实际上似乎一次打开5个浏览器实例(但是,这可能只与gui打开我猜测)。

很想知道这是真的并行运行,还是我需要在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 个答案:

答案 0 :(得分:2)

如果您在Linux上运行此命令,则可以使用There is no RNG shell命令查看正在运行的进程。打开时,如果按top,它将显示线程。在看Shift-H的同时运行程序,你会看到在一个父线程下运行的几个python线程。如果是这样,它就可以了。