【问题标题】:Running shell command in background without waiting在后台运行shell命令而无需等待
【发布时间】:2023-04-05 17:19:01
【问题描述】:

我想在我的python中同时运行多个命令,但是程序似乎在前一个命令完成时执行了一个命令,

这是我的代码:

import os
import glob
for i in (glob.glob("PATH/SUB/*.csv")):
    file = i.split('\\')[1]
os.system("scrapy crawl quotes -a file=%s -o /OUTPUT/%s.csv &" % (file,file)) 

【问题讨论】:

    标签: python shell operating-system


    【解决方案1】:

    简答:

    #!/usr/bin/env python3.6
    import glob
    import asyncio
    
    
    async def run(shell_command):
        p = await asyncio.create_subprocess_shell(shell_command)
        await p.communicate()
    
    
    async def main(shell_commands):
        for f in asyncio.as_completed([run(c) for c in shell_commands]):
            await f
    
    
    commands = []
    for i in (glob.glob("PATH/SUB/*.csv")):
        file = i.split('\\')[1]
        commands.append("scrapy crawl quotes -a file=%s -o /OUTPUT/%s.csv &" % (file, file))
    
    loop = asyncio.ProactorEventLoop() 
    loop.run_until_complete(main(commands))
    loop.close()
    

    长答案:

    您可以检查 asyncio 子进程。

    让我们测试一下。我们将尝试运行 5 次“sleep 5”命令 - 我们预计总运行时间将接近 5 秒(而不是 25 秒)。

    #!/usr/bin/env python3.6
    import time
    import asyncio
    from asyncio.subprocess import PIPE, STDOUT
    
    
    async def run(shell_command):
        p = await asyncio.create_subprocess_shell(shell_command, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
        return (await p.communicate())[0].splitlines()
    
    
    async def main():
        commands = [run('sleep 5; echo {i}'.format(i=i)) for i in range(5)]
        for f in asyncio.as_completed(commands):
            print(await f)
    
    
    start = time.time()
    
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
    loop.close()
    
    total = time.time() - start
    
    print("Total time {} s".format(total))
    

    输出:

    [b'2']
    [b'3']
    [b'4']
    [b'1']
    [b'0']
    Total time 5.0065038204193115 s
    

    一切如预期。

    根据您的需要调整后:

    #!/usr/bin/env python3.6
    import glob
    import asyncio
    
    
    async def run(shell_command):
        p = await asyncio.create_subprocess_shell(shell_command)
        await p.communicate()
    
    
    async def main(shell_commands):
        for f in asyncio.as_completed([run(c) for c in shell_commands]):
            await f
    
    
    commands = []
    for i in (glob.glob("PATH/SUB/*.csv")):
        file = i.split('\\')[1]
        commands.append("scrapy crawl quotes -a file=%s -o /OUTPUT/%s.csv &" % (file, file))
    
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main(commands))
    loop.close()
    

    最后,如果你使用的是 Windows,你需要改变

    loop = asyncio.get_event_loop()
    

    进入

    loop = asyncio.ProactorEventLoop() 
    

    我假设您使用的是 Windows,因为您的代码

    file = i.split('\\')[1].
    

    【讨论】:

      猜你喜欢
      • 2014-09-13
      • 2014-04-09
      • 2016-01-04
      • 2012-08-13
      • 1970-01-01
      • 2021-12-15
      • 2018-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多