O'Reilly logo

Python Parallel Programming Cookbook by Giancarlo Zaccone

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to manage a state between processes

Python multiprocessing provides a manager to coordinate shared information between all its users. A manager object controls a server process that holds Python objects and allows other processes to manipulate them.

A manager has the following properties:

  • It controls the server process that manages a shared object
  • It makes sure the shared object gets updated in all processes when anyone modifies it

How to do it...

Let's see an example of how to share a state between processes:

  1. First, the program creates a manager list, shares it between n number of taskWorkers, and every worker updates an index.
  2. After all workers finish, the new list is printed to stdout:
    import multiprocessing def worker(dictionary, key, item): dictionary[key] ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required