多用途python

2024-10-02 22:38:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要改进一个同时运行3个python脚本的脚本,我在下面准备好了这个脚本,只是它跳过了列表中的一些脚本,我不知道为什么会发生这种情况

代码:

import sys
import asyncio


scriptspy = [
    '/scipts/sp01.py',
    '/scipts/sp02.py',
    '/scipts/sp03.py', 
    '/scipts/sp04.py',
    '/scipts/sp05.py',
    '/scipts/sp06.py',
    '/scipts/sp07.py', 
    '/scipts/sp08.py',
    '/scipts/sp09.py',
    '/scipts/sp10.py',
    '/scipts/sp11.py', 
    '/scipts/sp12.py',
    '/scipts/sp13.py',
    '/scipts/sp14.py',
    '/scipts/sp15.py', 
    '/scipts/sp16.py',
    '/scipts/sp17.py',
    '/scipts/sp18.py',
    '/scipts/sp19.py', 
    '/scipts/sp20.py',
    '/scipts/sp21.py',
    '/scipts/sp22.py',
    '/scipts/sp23.py', 
    '/scipts/sp24.py',

]

#linux

async def main():
    task_run= set()
    while scriptspy:
        # start 3 scripts
        while len(task_run) < 3 and scriptspy:
            script = scriptspy.pop()
            p = await asyncio.create_subprocess_exec(sys.executable, script)
            task = asyncio.create_task(p.wait())
            task_run.add(task)
        # wait one and start
        task_run, task_end = await asyncio.wait(task_run, return_when=asyncio.FIRST_COMPLETED)
    await asyncio.wait(task_run, return_when=asyncio.ALL_COMPLETED)


asyncio.run(main())

细节 S.O执行:Linux(Ubuntu) Python:3.7版


Tags: andrunpyimport脚本asynciotaskmain