[Python] Setting global variables for python multiprocessing

Discussão em 'Python' iniciado por Stack, Agosto 27, 2025.

  1. Stack

    Stack Membro Participativo

    I have a large array and an object I'd like to call multiple times with multiprocessing. Neither the data nor the object internals get modified.

    This works:

    import numpy as np
    from multiprocessing import Pool

    data = np.arange(400).reshape(20, 20)

    class MyClass():
    def __call__(self, indx):
    return np.sum(data[:, indx])

    my_class = MyClass()

    def call_single_indx(indx):
    result = my_class(indx)
    return result

    def launch_jobs(nmap=10, num_jobs=3):
    with Pool(processes=num_jobs) as pool:
    result = pool.map(call_single_indx, range(nmap))
    result = np.array(result)
    return result

    if __name__ == "__main__":

    result = launch_jobs()
    print(result)


    But this fails:

    import numpy as np
    from multiprocessing import Pool

    data = None
    my_class = None

    def set_globals(n1, n2):
    global data
    data = np.arange(n1).reshape(n2, n2)
    global my_class
    my_class = MyClass()

    class MyClass():
    def __call__(self, indx):
    return np.sum(data[:, indx])

    def call_single_indx(indx):
    result = my_class(indx)
    return result

    def launch_jobs(nmap=10, num_jobs=3):
    with Pool(processes=num_jobs) as pool:
    result = pool.map(call_single_indx, range(nmap))
    result = np.array(result)
    return result

    if __name__ == "__main__":
    set_globals(n1=400, n2=20)
    launch_jobs()


    How can I get my set_globals function to actually set the global variables so that all the processes share them?

    I'm adding some more text here because the post is mostly code and it won't let me post without more details.

    Continue reading...

Compartilhe esta Página