Sharing States

One way to pass values between processes in Python is using shared memory. In the example below, the processes never terminate and there is no join.

from multiprocessing import Process, Value
import time

def matt(x):

    while True:
        print(".", end="")
        x.value += 1
        time.sleep(1)

def main():
    x = Value('d', 0)

    p = Process(target=matt, args=(x,))
    p.daemon = True
    p.start()
    # do some stuff
    # p.join()
    while True:
         time.sleep(5)
         print(f" {x.value}")

if __name__ == "__main__":
    main()

2025-01-06


copyright 2025 mobilarte