Python Glossary Русский

all Python 3multiprocessingfunction

multiprocessing.Manager()

pools and managers

Starts a server process (SyncManager) and returns a manager that creates shared dict, list, Queue, Lock and other objects accessed through proxies.

Syntax

with ctx.Manager() as manager: d = manager.dict(); lst = manager.list()

Every proxy access is a message exchange with the server, so it is hundreds of times slower than Value/Array, but works for arbitrary objects. The nesting trap: d['k'].append(1) changes a copy fetched from the server and the change is lost; take the value out, change it and assign it back. Unlike Lock and Queue, manager objects can also be passed through a Pool. Use with: on exit the server stops, and proxies stop working.

Examples

import multiprocessing as mp
def record(d, key):
    d[key] = key * 2
if __name__ == '__main__':
    ctx = mp.get_context('spawn')
    with ctx.Manager() as manager:
        d = manager.dict()
        ps = [ctx.Process(target=record, args=(d, k)) for k in (1, 2)]
        for p in ps:
            p.start()
        for p in ps:
            p.join()
        print(sorted(d.items()))  # → [(1, 2), (2, 4)]
import multiprocessing as mp
if __name__ == '__main__':
    with mp.get_context('spawn').Manager() as manager:
        d = manager.dict({'k': []})
        d['k'].append(1)  # меняется копия, а не общий список
        print(d['k'])  # → []
        items = d['k']
        items.append(1)
        d['k'] = items  # присваивание отправляет новое значение серверу
        print(d['k'])  # → [1]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy