Processamento multithreading

mail@pastecode.io avatar
unknown
python
7 months ago
3.4 kB
2
Indexable
Never
import multiprocessing
import time
import tracemalloc
from time import sleep

from applicationrpa.selenium import ChromeDriver
from selenium.webdriver.common.by import By

import utils
from logger import log

# Habilitar o tracemalloc
tracemalloc.start()


# Função que será executada em cada processo
def worker_function(process_id, sublist):
    # Função de teste
    driver = ChromeDriver(['--headless', '--disable-extensions', '--disable-notifications', '--disable-dev-shm-usage'],
                          initial_url=utils.get_auth_url())

    log(f"Processo {process_id} iniciado... {driver.driver.current_url}")

    i = 0
    for value in sublist:
        i += 1
        driver.driver.get(utils.get_at_acq_url())
        try:
            driver.find_element(
                By.XPATH,
                '//*[@id="wizard-p-0"]/fieldset[1]/div[1]/input',
                exception=True,
                retries=3,
                interval=1
            ).send_keys(value)
        except Exception as e:
            log(f"#{i}/{len(sublist)} Processo {process_id} ... [ {driver.driver.title} - {e} ]")
            continue

        text = driver.find_element(
            By.XPATH,
            '/html/body/table[1]/tbody/tr/td[3]/table/tbody/tr/td[1]/span',
            exception=False,
            retries=3,
            interval=1
        )

        sleep(0.5)
        
        user_test = text.text if text is not None else None
        log(f"#{i}/{len(sublist)} Processo {process_id} ... [ {driver.driver.title} - {user_test} ]")

    log(f"Processo {process_id} finalizado...")
    driver.quit()


def main():
    # Habilitar o tracemalloc para capturar o consumo de memória
    tracemalloc.start()

    start_time = time.time()

    # Crie uma lista com 331 elementos (Simular uma lista de 331 processos)
    original_list = []
    for i in range(331):
        original_list.append(f'teste-{i}')

    # Divida a lista original em {div_value} sublistas
    div_value = 1
    # Calcule o tamanho das sublistas
    sublist_size = len(original_list) // div_value
    remaining_elements = len(original_list) % div_value

    sublists = []

    start = 0
    for i in range(div_value):
        sublist_end = start + sublist_size + (1 if i < remaining_elements else 0)
        sublists.append(original_list[start:sublist_end])
        start = sublist_end

    # Execução dos processos em paralelo
    processes = []
    process_id = 0
    for sublist in sublists:
        process_id += 1

        # Crie um processo para cada sublista
        process = multiprocessing.Process(target=worker_function, args=(process_id, sublist,),
                                          name=f"Processo {process_id}")
        processes.append(process)

        # Inicie o processo
        process.start()

    # Aguarde todos os processos terminarem
    for process in processes:
        process.join()

    print("--- %s seconds ---" % (time.time() - start_time))

    # Capturar o consumo de memória
    snapshot = tracemalloc.take_snapshot()
    top_stats = snapshot.statistics('lineno')
    for stat in top_stats[:10]:
        log(stat)

    # Desabilitar o tracemalloc
    tracemalloc.stop()

    log("Todos os processos terminaram")


if __name__ == "__main__":
    main()