Skip to main content

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

Nombre Tipo Requerido Descripción
key str SI La clave en el diccionario compartido
value None Si El valor a asignar a la clave

Return 

Tipo
None

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.")