# threads



# _single_thread

**Descripción**

Sólo para uso interno: Ejecuta una función dada en un hilo.

**Parámetros**

<table border="1" id="bkmrk-module-acepta-el-tip" style="width: 426px;"><tbody><tr><td style="width: 73px;">**Nombre** </td><td style="width: 61px;">**Tipo**</td><td style="width: 95px;">**Requerido**</td><td style="width: 197px;">**Descripción**</td></tr><tr><td style="width: 73px;">q</td><td style="width: 61px;">None</td><td style="width: 95px;">Si</td><td style="width: 197px;">Una cola de la que obtener parámetros para la función</td></tr><tr><td style="width: 73px;">function</td><td style="width: 61px;">callable</td><td style="width: 95px;">Si</td><td style="width: 197px;">La función a ejecutar en el hilo</td></tr><tr><td style="width: 73px;">errors</td><td style="width: 61px;">list</td><td style="width: 95px;">No</td><td style="width: 197px;">Una lista para almacenar cualquier error encontrado durante la ejecución</td></tr></tbody></table>

**Versión**

- 1.0.0

# run_threads

**Descripción**

Ejecutar una función dada para una lista de elementos dada en un número dado de hilos

**Parámetros**

<table border="1" id="bkmrk-module-acepta-el-tip" style="width: 562px; height: 162px;"><tbody><tr style="height: 29px;"><td style="width: 112px; height: 29px;">**Nombre**</td><td style="width: 70px; height: 29px;">**Tipo**</td><td style="width: 88px; height: 29px;">**Requerido**</td><td style="width: 292px; height: 29px;">**Descripción**</td></tr><tr style="height: 46px;"><td style="width: 112px; height: 46px;"><div><div>max_threads</div></div></td><td style="width: 70px; height: 46px;">int</td><td style="width: 88px; height: 46px;">SI</td><td style="width: 292px; height: 46px;">Número de hilos </td></tr><tr style="height: 29px;"><td style="width: 112px; height: 29px;">function</td><td style="width: 70px; height: 29px;">callable</td><td style="width: 88px; height: 29px;">Si</td><td style="width: 292px; height: 29px;">Función a ejecutar</td></tr><tr style="height: 29px;"><td style="width: 112px; height: 29px;">items</td><td style="width: 70px; height: 29px;">list</td><td style="width: 88px; height: 29px;">Si</td><td style="width: 292px; height: 29px;">Lista con los items que se asignaran a cada hilo</td></tr><tr style="height: 29px;"><td style="width: 112px; height: 29px;"><div><div>print_errors</div></div></td><td style="width: 70px; height: 29px;">bool</td><td style="width: 88px; height: 29px;">No</td><td style="width: 292px; height: 29px;">Para pintar error por salida</td></tr></tbody></table>

**Return**

<table border="1" id="bkmrk-module_xml-m%C3%B3dulo-en" style="width: 557px;"><tbody><tr><td style="width: 66px;">bool</td><td style="width: 491px;">Devuelve True si no ha habido errores en la ejecución y 0 de suceder lo contrario.</td></tr></tbody></table>

<div id="bkmrk-"></div>**Versión**

- 1.0.0

**Ejemplo**

```
import pandoraPlugintools as pt

# Define a function to be executed in each thread
def process_item(item):
    # Perform some processing on the item
    print(f"Processing item: {item}")

# List of items to process
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Number of threads to use
max_threads = 3

# Run the function for each item using multiple threads
success = pt.run_threads(max_threads=max_threads, function=process_item, items=items, print_errors=True)

if success:
    print("All threads executed successfully.")
else:
    print("Errors occurred during thread execution.")
```

# set_shared_dict_value

**Descripción**

Establece un valor para una clave en el diccionario interno compartido. Esta función es utilizada por todos los procesos paralelos.

**Parámetros**

<table border="1" id="bkmrk-module-acepta-el-tip" style="width: 562px;"><tbody><tr><td style="width: 70px;">**Nombre**</td><td style="width: 54px;">**Tipo**</td><td style="width: 90px;">**Requerido**</td><td style="width: 348px;">**Descripción**</td></tr><tr><td style="width: 70px;">key</td><td style="width: 54px;">str</td><td style="width: 90px;">SI</td><td style="width: 348px;">La clave en el diccionario compartido</td></tr><tr><td style="width: 70px;">value</td><td style="width: 54px;">None</td><td style="width: 90px;">Si</td><td style="width: 348px;">El valor a asignar a la clave</td></tr></tbody></table>

<div id="bkmrk-"></div>**Return**

<div id="bkmrk-tipo-none"><table border="1" style="border-collapse: collapse; width: 100%;"><tbody><tr><td style="width: 100%;">**Tipo**</td></tr><tr><td style="width: 100%;">None</td></tr></tbody></table>

</div>**Versión**

- 1.0.0

Ejemplo

```
import pandoraPlugintools as pt

# Define a function to be executed by parallel processes
def process_data(item):
    # Process the data and set a value in the shared dictionary
    result = item * 2
    pt.set_shared_dict_value(key=str(item), value=result)

# List of items to process
data_list = [1, 2, 3, 4, 5]

# Create a shared dictionary and lock for synchronization
shared_dict = multiprocessing.Manager().dict()
shared_dict_lock = multiprocessing.Manager().Lock()

# Set the shared dictionary and lock in the module
pt._SHARED_DICT = shared_dict
pt._SHARED_DICT_LOCK = shared_dict_lock

# Number of parallel processes to use
num_processes = 2

# Run the function for each item using parallel processes
success = pt.run_parallel(num_processes=num_processes, function=process_data, data_list=data_list, print_errors=True)

if success:
    print("All processes executed successfully.")
else:
    print("Errors occurred during process execution.")
```

# add_shared_dict_value

**Descripción**

Añade un valor a una clave del diccionario interno compartido. Esta función es utilizada por todos los procesos paralelos.

**Parámetros**

<table border="1" id="bkmrk-module-acepta-el-tip" style="width: 562px; height: 87px;"><tbody><tr style="height: 29px;"><td style="width: 70px; height: 29px;">**Nombre**</td><td style="width: 54px; height: 29px;">**Tipo**</td><td style="width: 90px; height: 29px;">**Requerido**</td><td style="width: 348px; height: 29px;">**Descripción**</td></tr><tr style="height: 29px;"><td style="width: 70px; height: 29px;">key</td><td style="width: 54px; height: 29px;">str</td><td style="width: 90px; height: 29px;">Si</td><td style="width: 348px; height: 29px;">La clave en el diccionario compartido</td></tr><tr style="height: 29px;"><td style="width: 70px; height: 29px;">value</td><td style="width: 54px; height: 29px;">None</td><td style="width: 90px; height: 29px;">SI</td><td style="width: 348px; height: 29px;">El valor a añadir a la clave</td></tr></tbody></table>

<div id="bkmrk-"></div>**Return**

<div id="bkmrk-tipo-none"><table border="1" style="border-collapse: collapse; width: 100%;"><tbody><tr><td style="width: 100%;">**Tipo**</td></tr><tr><td style="width: 100%;">None</td></tr></tbody></table>

</div>**Versión**

- 1.0.0

**Ejemplo**

```
import pandoraPlugintools as pt

# Define a function to be executed by parallel processes
def process_data(item):
    # Process the data and add a value to the shared dictionary
    result = item * 2
    pt.add_shared_dict_value(key=str(item), value=result)

# List of items to process
data_list = [1, 2, 3, 4, 5]

# Create a shared dictionary and lock for synchronization
shared_dict = multiprocessing.Manager().dict()
shared_dict_lock = multiprocessing.Manager().Lock()

# Set the shared dictionary and lock in the module
pt._SHARED_DICT = shared_dict
pt._SHARED_DICT_LOCK = shared_dict_lock

# Number of parallel processes to use
num_processes = 2

# Run the function for each item using parallel processes
success = pt.run_parallel(num_processes=num_processes, function=process_data, data_list=data_list, print_errors=True)

if success:
    print("All processes executed successfully.")
else:
    print("Errors occurred during process execution.")
In this example, the process_data function processes each item and adds a value to the shared dictionary using the add_shared_dict_value function. The run_parallel function is used to execute the process_data function in parallel processes for the given list of items. The shared dictionary and lock are created using the multiprocessing.Manager() and then assigned to the corresponding module variables _SHARED_DICT and _SHARED_DICT_LOCK in the pandoraPluginTools module.






```

# get_shared_dict_value

**Descripción**

Obtener el valor de una clave en el diccionario interno compartido. Esta función es utilizada por todos los procesos paralelos.

**Parámetros**

<table border="1" id="bkmrk-module-acepta-el-tip" style="width: 562px;"><tbody><tr><td style="width: 70px;">**Nombre**</td><td style="width: 54px;">**Tipo**</td><td style="width: 90px;">**Requerido**</td><td style="width: 348px;">**Descripción**</td></tr><tr><td style="width: 70px;">key</td><td style="width: 54px;">str</td><td style="width: 90px;">Si</td><td style="width: 348px;">La clave en el diccionario compartido</td></tr></tbody></table>

<div id="bkmrk-"></div>**Return**

<div id="bkmrk-tipo-none"><table border="1" style="border-collapse: collapse; width: 100%;"><tbody><tr><td style="width: 100%;">**Tipo**</td></tr><tr><td style="width: 100%;">None</td></tr></tbody></table>

</div>**Versión**

- 1.0.0

**Ejemplo**

```
import pandoraPlugintools as pt

# Define a function to be executed by parallel processes
def process_data(item):
    # Process the data and get the value from the shared dictionary
    result = item * 2
    value = pt.get_shared_dict_value(key=str(item))
    print(f"Item: {item}, Processed Value: {result}, Shared Value: {value}")

# List of items to process
data_list = [1, 2, 3, 4, 5]

# Create a shared dictionary and lock for synchronization
shared_dict = multiprocessing.Manager().dict()
shared_dict_lock = multiprocessing.Manager().Lock()

# Set the shared dictionary and lock in the module
pt._SHARED_DICT = shared_dict
pt._SHARED_DICT_LOCK = shared_dict_lock

# Number of parallel processes to use
num_processes = 2

# Run the function for each item using parallel processes
success = pt.run_parallel(num_processes=num_processes, function=process_data, data_list=data_list, print_errors=True)

if success:
    print("All processes executed successfully.")
else:
    print("Errors occurred during process execution.")
```

# run_processes

**Descripción**

Ejecuta una función dada para una lista de elementos dada en un número dado de procesos

**Parámetros**

<table border="1" id="bkmrk-module-acepta-el-tip" style="width: 562px;"><tbody><tr><td style="width: 70px;">**Nombre**</td><td style="width: 54px;">**Tipo**</td><td style="width: 90px;">**Requerido**</td><td style="width: 348px;">**Descripción**</td></tr><tr><td style="width: 70px;"><div><div>max_processes</div></div></td><td style="width: 54px;">int</td><td style="width: 90px;">Si</td><td style="width: 348px;">El número máximo de procesos a ejecutar en paralelo</td></tr><tr><td style="width: 70px;"><div><div>function</div></div></td><td style="width: 54px;">callable</td><td style="width: 90px;">SI</td><td style="width: 348px;">La función a ejecutar para cada ítem</td></tr><tr><td style="width: 70px;"><div><div>items</div></div></td><td style="width: 54px;">list</td><td style="width: 90px;">Si</td><td style="width: 348px;">Lista de ítems a procesar</td></tr><tr><td style="width: 70px;"><div><div>print_errors</div></div></td><td style="width: 54px;">bool</td><td style="width: 90px;">No</td><td style="width: 348px;">Si se van a imprimir los errores</td></tr></tbody></table>

**Return**

<table border="1" id="bkmrk-module_xml-m%C3%B3dulo-en" style="width: 558px;"><tbody><tr><td style="width: 52px;">**Tipo**</td><td style="width: 506px;">**Descripción**</td></tr><tr><td style="width: 52px;">bool</td><td style="width: 506px;">True si todos los procesos se completaron con éxito, False en caso contrario</td></tr></tbody></table>

<div id="bkmrk-"></div>**Versión**

- 1.0.0

```
import pandoraPlugintools as pt

# Define a function to be executed by parallel processes
def process_data(item):
    result = item * 2
    print(f"Item: {item}, Processed Value: {result}")

# List of items to process
data_list = [1, 2, 3, 4, 5]

# Number of parallel processes to use
num_processes = 2

# Run the function for each item using parallel processes
success = pt.run_processes(max_processes=num_processes, function=process_data, items=data_list, print_errors=True)

if success:
    print("All processes completed successfully.")
else:
    print("Errors occurred during process execution.")
```