[Python] Parallel processing with arguments to modify

Discussão em 'Python' iniciado por Stack, Abril 1, 2025.

  1. Stack

    Stack Membro Participativo

    I need to parallelize a function that modifies values of one of the arguments. For example modify this simple code:

    def Test(i, a, length):
    if i % 2 == 0:
    for x in range(0, length, 2):
    a[x] = 2
    else:
    for x in range(1, length, 2):
    a[x] = 1

    a0 = numpy.ndarray(shape=10, dtype=numpy.uint8)
    Test(0, a, a.shape[0])
    Test(1, a, a.shape[0])
    print(a)


    I tried to use joblib using Parallel(n_jobs=2)(delayed(Test)(i, a, 10) for i in range(2)) and multiprocessing.Pool with:

    with multiprocessing.Pool(processes=2) as Pool:
    data = [(0, a, a.shape[0]), (1, a, a.shape[0])]
    Pool.starmap(Test, data)


    But both solutions do not work because they fork the data, so the parameters do not get modified.

    What would be my options to do such parallelization?

    Continue reading...

Compartilhe esta Página