简体   繁体   中英

Run processes in parallel with python on windows asyncio

I have a code that only runs on Linux with asyncio, but I need it to run on Windows, my knowledge is little in multiprocessing, what would be the way to run on Windows? I saw that there is a limitation of asyncio to run on windows: https://docs.python.org/3/library/asyncio-platforms.html#asyncio-windows-subprocess

My code:

import sys
import asyncio


scriptspy = [
    'scrp1.py',
    'scrp2.py',
    'scrp3.py',
    'scrp4.py',
    'scrp5.py',
    'scrp6.py',
    'scrp7.py',
    'scrp8.py',
    'scrp9.py',
    'scrp10.py',
    'scrp11.py',
    'scrp12.py',
]

async def main():
    tarefas_rodando = set()
    while scriptspy:
        # start up to 6 scripts
        while len(tarefas_rodando) < 6 and scriptspy:
            script = scriptspy.pop()
            p = await asyncio.create_subprocess_exec(sys.executable, script)
            tarefa = asyncio.create_task(p.wait())
            tarefas_rodando.add(tarefa)
        # wait for one of the scripts to end
        tarefas_rodando, finalizadas = await asyncio.wait(tarefas_rodando, return_when=asyncio.FIRST_COMPLETED)
    # finished, wait for the rest to finish
    await asyncio.wait(tarefas_rodando, return_when=asyncio.ALL_COMPLETED)


asyncio.run(main())

This code runs fine on linux but not windows. I need to run it on windows. https://docs.python.org/3/library/asyncio-platforms.html#asyncio-windows-subprocess

Exception run in Windows:

Traceback (most recent call last):
   File "scripts.py", line 53, in <module>
    asyncio.run(main())
   File "C:\Python\Python37\lib\asyncio\runners.py", line 43, in run
    return loop.run_until_complete(main)
   File "C:\Python\Python37\lib\asyncio\base_events.py", line 584, in run_until_complete
    return future.result()
   File "scripts.py", line 44, in main
    p = await asyncio.create_subprocess_exec(sys.executable, script)
   File "C:\Python\Python37\lib\asyncio\subprocess.py", line 217, in create_subprocess_exec
    stderr=stderr, **kwds)
   File "C:\Python\Python37\lib\asyncio\base_events.py", line 1533, in subprocess_exec
    bufsize, **kwargs)
   File "C:\Python\Python37\lib\asyncio\base_events.py", line 463, in
_make_subprocess_transport
    raise NotImplementedError NotImplementedError

According to the documentation , to run subprocesses on Windows, you need to switch to the proactor event loop:

asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM