Async execution โ
Run the same commands from asyncio code.
Run commands with asyncio โ
python
import asyncio
from io import StringIO
from proclet import Command
async def uptimes(hosts: list[str]) -> dict[str, str]:
ssh = Command("ssh").timeout(10)
outputs = {host: StringIO() for host in hosts}
await asyncio.gather(
*(
ssh.copy().args(host, "uptime").stdout(out).aexec()
for host, out in outputs.items()
)
)
return {host: out.getvalue().strip() for host, out in outputs.items()}
print(asyncio.run(uptimes(["alpha", "beta", "gamma"])))- Every way of running a command has an
asynciocounterpart prefixed witha:aexec(),apipe()andapipeline(). They return anasyncio.subprocess.Process. - The configuration is shared: the same
Commandruns with either. sshis looked up onPATHonce;copy()gives each host its own command with the shared settings, so their args and redirects don't overwrite each other.