1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Multiprocessing on python

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

  1. Stack

    Stack Membro Participativo

    I have a task which takes about 10 seconds to run on a single core. I had the idea of using parallelization to improve the performance (if I can run 4 instances of the task in ten seconds I will be able to make some smarter decisions).

    So finally I write the following code:

    from multiprocessing import Pool, Process, Queue
    from vrp import task
    import time

    def task_paralela(queue, parameter):
    ret = task(parameter)
    queue.put(ret)

    if __name__ == '__main__':
    start = time.time()
    cores = 4
    parameter = 1
    processes = []
    q = Queue()
    rets = []
    for i in range(cores):
    p = Process(target=task_pararela, args=(q, parameter))
    processes.append(p)
    p.start()
    for p in processes:
    ret = q.get()
    rets.append(ret)
    for p in processes:
    p.join()
    end = time.time()
    total_time = end - start
    print(total_time)


    And it works, but the time is now near 20 seconds (twice as much). Why is that? Where is my mistake?

    Continue reading...

Compartilhe esta Página