0
import time
from multiprocessing import Pool, RawArray, sharedctypes
from ctypes import c_int



def init_worker(X):
    print(f"{X}")


def worker_func(i):
    print(f"{X}")
    time.sleep(i) # Some heavy computations
    return 

# We need this check for Windows to prevent infinitely spawning new child
# processes.
if __name__ == '__main__':

    X =sharedctypes.RawValue(c_int)
    X=3

    with Pool(processes=4, initializer=init_worker, initargs=(X)) as pool:
        pool.map(worker_func, [1,2,3,4])


    print(X)

--- I am simply trying to print the value of X in each subprocess. This is a toy program in order to check whether I can share a value and update it by using multiple processes.

1 Answers1

0

This program spawns infinite number of processes because it there is no ,(comma) after the X in initargs=(X) ; it should be initargs=(X,) . That is because, if the comma is left-out that causes an error.