M
M
Mikhail Trainin2019-05-11 11:49:24
Python
Mikhail Trainin, 2019-05-11 11:49:24

Multiprocessing and dictionaries how to process?

Task:
You need to initialize the process, run the handler function, get the last value from the list using pop (), the list itself is the value to a certain key in the dictionary, process this value and close the process.
Form:
Starting the process - Calling the handler function - Calling a function to get the value from the list in the dictionary by key - Processing the value - Closing the process
Problem:
When the process is initialized, it (the process). as if, it duplicates the class and works with its values ​​separately from the rest of the program and other processes, without synchronizing the data, from which the whole program constantly processes the same value. And pop() itself does not work correctly, returning the last value from the list each time, but not deleting it.
What I tried to do:
Value, Array, manager.dict(), global, update the dictionary manually, give up Process on threading.
Class structure:

class Example():

    dict = {}

    def get_param(self, n):
        #получение значения

    def wrapper(self, n):
        #На вход принимает ключ к словарю
        param = self.get_param(n)
        #обработка param и тд

    def start(self):
        #Запускает процесс который вызывает функцию proc

    def proc(self):
        #Запускает процесс который вызывает функцию обработчик

Code that does NOT work:
import multiprocessing

dicts = {}

def gen(n):
    list = dicts[str(n)]
    print(list)
    if len(list) > 0:
        return list.pop()


def proc(n, dicte):
    global dicts
    dicte['10'] = ['hello', 'how are', 'you']
    dicts = dicte
    a = gen(n)
    print(a)
    b = gen(n)
    print(b)


if __name__ == '__main__':
    manager = multiprocessing.Manager()
    dict = manager.dict()
    p1 = multiprocessing.Process(target=proc, args=(10, dict))
    p1.start()
    p1.join()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-05-11
@Stormx480

There is a process pool and its map() method specifically for this .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question