Skip to main content

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

Nombre Tipo Requerido Descripción
key str Si La clave en el diccionario compartido
value None SI El valor a añadir 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 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.