[Python] Running the same function multiple times concurrently

Discussão em 'Python' iniciado por Stack, Janeiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    So what I want to do is run the same function multiple times simultaneously while getting a result as return and storing it in an array or list. It goes like this:

    def base_func(matrix,arg1,arg2):
    result = []
    for row in range(matrix.shape[0]):
    #perform necessary operation on row and return a certain value to store it into result
    x = func(matrix[row],arg1,arg2)
    result.append(x)
    return np.array(result)


    I tried using threading in python. My implementation goes:

    def base_func(matrix,arg1,arg2):
    result = []
    threads = []
    for row in range(matrix.shape[0]):
    t = threading.Thread(target=func,args=(matrix[row],arg1,arg2,))
    threads.append(t)
    t.start()
    for t in threads:
    res = t.join()
    result.append(res)
    return np.array(result)


    This doesn't seem to work and just returns None from the threads.

    Continue reading...

Compartilhe esta Página