How to use threadpoolexecutor to run two tests and send the result of one as a parameter to the other in python

26 Views Asked by At

I am using ThreadPoolExecutor to run two tests simultaneously in python, for some tests, however I have a problem, and that is that let's say there is a main test and a secondary test, both will be executed from the beginning, however the secondary test will finish first, I return the result, to later use it as a parameter in the main test, since by the time I require it I have already finished the secondary test, but I don't see how to apply that.

I am using Selenium to do the test, and from my main.py file I send it to call the methods, so I have my code.

from concurrent.futures import ThreadPoolExecutor

def PruebaPrincipal(opcion_sistema, opcion_ambiente, opcion_usuario, opcion_especifica, tipo_de_credito, resultado_folio):
                        resultado_folio = resultado_folio_future.result()
                        with Flujos_obligatorios_originacion() as bot1:
                            limpiar_pantalla()
                            bot1.get_url(opcion_sistema, opcion_ambiente)
                            bot1.iniciar_sesion(opcion_ambiente, opcion_usuario)
                            bot1.Crear_Solicitud(tipo_de_credito)
                            resultado_database = bot1.Establecer_base_de_datos(opcion_ambiente)
                            curp_cliente = bot1.Llenar_Formulario_Solicitud(opcion_sistema, opcion_especifica, resultado_database, opcion_ambiente)
                            bot1.Solicitud_Tipo_de_Credito(tipo_de_credito)
                            bot1.Solicitud_Datos_del_Credito(resultado_folio, resultado_database, curp_cliente, tipo_de_credito)
                            bot1.cerrar_sesion()
                            bot1.close()

def PruebaSecundaria(opcion_ambiente, opcion_sistema):
                       with Flujos_obligatorios_originacion() as bot2:
                            resultado_folio_future = bot2.Obtener_Folio_Nav(opcion_ambiente, opcion_sistema)
                            return resultado_folio_future

with ThreadPoolExecutor(max_workers=2) as executor:
                       resultado_folio_future = executor.submit(PruebaSecundaria, opcion_ambiente, opcion_sistema)
                       executor.submit(PruebaPrincipal, opcion_sistema, opcion_ambiente, opcion_usuario, opcion_especifica, tipo_de_credito, resultado_folio_future)

I hope that the secondary test obtains a value for me to use in the main test, sending it as a parameter.

1

There are 1 best solutions below

2
AKX On

Sure. You get a Future from executor.submit(); calling .result() on a future waits for the future to have completed and returns the value (or raises an exception if the future failed).

from concurrent.futures import ThreadPoolExecutor, Future


def Suma(valor1: int, valor2: int):
    resultado_suma = valor1 + valor2
    print(f"El resultado de la suma es: {resultado_suma}")
    return resultado_suma


def Multiplicacion(resultado_suma: Future):
    resultado_multiplicacion = resultado_suma.result() * 2
    print(f"El resultado de la multiplicacion es: {resultado_multiplicacion}")
    return resultado_multiplicacion + 15


with ThreadPoolExecutor(max_workers=2) as executor:
    sum_future = executor.submit(Suma, 5, 5)
    mult_future = executor.submit(Multiplicacion, sum_future)
    print("Final result:", mult_future.result())

This prints out

El resultado de la suma es: 10
El resultado de la multiplicacion es: 20
Final result: 35