ZeroToOne
Monday, Nov 20, 2017

Python Shared Data Between Processes

Providing a shared data or state between different processes is so useful and great idea because in many cases when you want to implement parallel systems, you should provide a way that each process can communicate with others.

Fortunately, Python supports many kinds of shared data such as Value, Array, Dict, and Queue and so on.

Let’s try an example:

We have 2 processes which are:

  • Process-1 is responsible to check the value (shared data).
  • Process-2 is responsible to set the value (shared data).
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from multiprocessing import Process, Value
from ctypes import c_char
from time import sleep


def process_1(shared_data):
    """Process-1 is responsible to check the value."""
    while shared_data.value != b"my_data":
      sleep(0.5)
    print("process-1 got the value. exit.")

def process_2(shared_data):
    """Process-2 is responsible to set value."""
    sleep(1)
    shared_data.value = b"my_data"
    sleep(1)
    print("process-2 set the value. exit.")

if __name__ == "__main__":
    # create a shared data with 10 character length
    shared_data = Value(c_char * 10)
    p1 = Process(target=process_1, args=(shared_data,))
    p2 = Process(target=process_2, args=(shared_data,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

Try to implement your projects and idea with this great features which help you to have high performance and scalable systems.

References:

All rights reserved by Morteza NourelahiAlamdari (mortymacs) ©
All contents are under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.
Top banner picture by Unsplash.