Loop to perform quantity processes in quantity

Asked

Viewed 134 times

0

I have a size 30 list in python, I need to make a loop to catch 6 item from that run list, after we finish the 6 run over 6 and so on until finalizing the list.

but I’m not finding a way to do that.

   import os
    scripts[
    'exp1.py'
    'exp2.py'
    'exp3.py'
    'exp4.py'
    [...]
    'exp30.py'
    ]
def roda_processo(processo):
    os.system('python {}'.format(processo))

execute:

from multiprocessing import Pool
from scripts import *

if __name__ == '__main__':
    num_processors = #[preciso que a execução ocorra aqui 6 em 6]

    pool = Pool(processes=num_processors)
    output = pool.map(roda_processo, processos)
    print(output)

Because I need you to only run 6 processes at a time and when you finish one run the other.

  • I didn’t get it right. The exp.py is the bottom code? Where are the 30 elements of the list? You want 6 in 6 because the num_processors is 6 or has to be 6 in 6 regardless of the num_processors?

  • I have a list with 30 elements scripts need to execute this list of processes 6 in 6, because it has to be 6 in 6 can not run the 30 at a time.

  • What does his code do today? And what does he not do? What should he do?

  • today runs all scripts at once. I need to run 6 by 6 and as I finish a two 6 picks up another and runs until I complete all of the list.

1 answer

4

No need to use multiprocessing to start a process that will use os.system to run a shell (another process) which in turn will run another python process to start a script!!

This way you are running 3 processes for each job you want to perform! Very inefficient and confusing.

Instead use the module asyncio. The advantage is to be able to make use of asyncio.wait() to wait for one of the processes to end; thus it is possible to start a new process as soon as any of them ends, in any order, even if one of the processes is faster than the other.

import sys
import asyncio

scripts = [
    'exp1.py',
    'exp2.py',
    'exp3.py',
    'exp4.py',
    ...
    'exp30.py',
]

async def main():
    tarefas_rodando = set() 
    while scripts:
        # inicia até 6 scripts
        while len(tarefas_rodando) < 6 and scripts:
            script = scripts.pop()
            p = await asyncio.create_subprocess_exec(sys.executable, script)
            tarefa = asyncio.create_task(p.wait())
            tarefas_rodando.add(tarefa)

        # espera um dos scripts acabar
        finalizadas, tarefas_rodando = await asyncio.wait(tarefas_rodando, 
            return_when=asyncio.FIRST_COMPLETED)

    # finalizou, espera o restante terminar
    await asyncio.wait(tarefas_rodando, return_when=asyncio.ALL_COMPLETED)

if sys.platform == "win32":
    asyncio.set_event_loop_policy(
        asyncio.WindowsProactorEventLoopPolicy())

asyncio.run(main())
  • I understood, it is that I had never worked several processes this way.

  • I executed the script and gave an error: https://gist.github.com/braganetx/210d8873d9b060948b1eeefaa8bc637d

  • @JB_ looks like you are in windows, read this section of the documentation from asyncio on use of subprocesses in windows: https://docs.python.org/3/library/asyncio-platforms.html#asyncio-windows-subprocess

  • Yes I am using Windows, I will mount a linux environment to run this process, because it on the server will run under linux.

  • I managed to migrate to Linux, but it is detaching process: /usr/lib/python3.7/asyncio/unix_events.py:861: RuntimeWarning: A loop is being detached from a child watcher with pending handlers&#xA; RuntimeWarning)&#xA; I did not understand why this occurred, I searched for this error and I saw that it may have to do with internet connection, but it is not my case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.