q0987
q0987

Reputation: 35982

How to update global variable in python multiprocessing

#!/usr/bin/env python
import signal
import time
import sys
import multiprocessing

idx = 0

def worker_function():
    global idx
    idx = idx + 1
    print("idx = {0}\n".format(idx))
    time.sleep(1)


def main():
    for i in range(3):
        worker = multiprocessing.Process(target=worker_function, args=())
        worker.start()
        worker.join()


if __name__ == "__main__":
    main()

output is as follows:

idx = 1

idx = 1

idx = 1

Question> Why the python global variable cannot be updated by each individual process?

Upvotes: 0

Views: 5832

Answers (2)

Ravi Srivastava
Ravi Srivastava

Reputation: 41

This is the right way to update a shared variable.

from multiprocessing import Process, Value, Array
import signal
import time
import sys

def handler(signal, frame):
    print("Final idx in handler = {0}\n".format(idx.value))

idx = Value('i', 10)

def worker_function():
    with idx.get_lock():
        idx.value = idx.value + 10
    print("idx = {0}\n".format(idx.value))
    time.sleep(1)
    signal.raise_signal(signal.SIGUSR1)
    sys.exit(0)

if __name__ == '__main__':
    signal.signal(signal.SIGUSR1, handler)
    for i in range(10):
        worker = Process(target=worker_function, args=())
        worker.start()
        #worker.join()
    print("In main, idx = {0}\n".format(idx.value))

Upvotes: 0

Gerard H. Pille
Gerard H. Pille

Reputation: 2578

Each process is updating its global variable, as you can see in the output. Else they would have shown "idx = 0". It's a complete other story if you want to share data between those processes, and obtain "idx = 1, 2, 3". See https://docs.python.org/3/library/multiprocessing.html#sharing-state-between-processes .

Upvotes: 2

Related Questions