简答:
#!/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].