Making conditions on returned value from first process

Hallow everyone, I am making a simple python program where i need a multiprocessing for starting two functions at the same time. Following is the main process of the program.

  1. creating two functions.
  2. making a global variable for storing value from function.
  3. starting two functions at the same time using multiprocessing library in python.
  4. Work of first function is to load and change the value of global variable at certain time.
  5. Work of second function is continuously printing specific data while global variable is not equal to certain value.

“”” import time import multiprocessing

a = "Wait"
def test1():
    print("n#######################nFunction 1")
    global a
    time.sleep(1)
    a = "Loaded Data"
    print(a)

def test2():
    print("n#######################nFunction 2")
    global a
    print(a)

if __name__ == '__main__':
    t1 = multiprocessing.Process(target=test1, args=[])
    t1.start()
    time.sleep(2)
    t2 = multiprocessing.Process(target=test2, args=[])
    t2.start()

“””

The output of following code is below:

“””

#######################
Function 1
Loaded Data
#######################
Function 2
Wait

“””

The problem I am facing here is that the first function executed successfully and printed the value of a. But while in second function it prints the old value of global variable.

I think that I have done something wrong. I need someone to help me. What is needed is :

  1. How to fix or change global variable value.

  2. How to use loops (conditions) on global variable in second function to for printing value!=specific value.

  3. Please correct my code and tell how use looping in second condition. If a!=”LoadedData” second function continuously prints “Loading Data …. Please Wait”. Else stop looping.

And also sorry for my poor and broken English Language

Answer

Processes have no shared memory by default in Python, although it’s possible to do it. Quoting from docs:

… when doing concurrent programming it is usually best to avoid using shared state as far as possible. This is particularly true when using multiple processes.

One way to do it is using Manager and Value.

from multiprocessing import Manager, Process
import time
import ctypes

def test1(a):
    print("n#######################nFunction 1")
    time.sleep(1)
    print(a.value)
    a.value = "Loaded Data"

def test2(a):
    print("n#######################nFunction 2")
    print(a.value)

if __name__ == '__main__':
    manager = Manager()
    a = manager.Value(ctypes.c_wchar_p, "Wait")

    t1 = Process(target=test1, args=(a,))
    t1.start()
    time.sleep(2)
    t2 = Process(target=test2, args=(a,))
    t2.start()
    t1.join()
    t2.join()

The reason you get the “old” value of the global a variable is that every process gets it’s own copy of a. The second process is fully new instance of the Python interpreter, with no knowledge about the first process. I suggest you read the docs first.