Python 3.4+Async programmingfunction
asyncio.create_subprocess_exec()
subprocesses
Starts a program as a child process without blocking the event loop; arguments are passed one by one, without a shell.
Syntax
proc = await asyncio.create_subprocess_exec(program, *args, stdin=None, stdout=None, stderr=None, **kwds)
Arguments go as separate parameters: create_subprocess_exec('ls', '-l'), not the string 'ls -l'. With stdout=PIPE read the output via await proc.communicate(): calling only proc.wait() deadlocks forever once the child fills the pipe buffer. Use sys.executable, not the string 'python', for the current interpreter. Windows needs ProactorEventLoop, the default since 3.8.
Examples
import asyncio
import sys
async def main():
proc = await asyncio.create_subprocess_exec(
sys.executable, '-c', 'print(6 * 7)',
stdout=asyncio.subprocess.PIPE,
)
out, _ = await proc.communicate() # не wait(): тот зависнет на полном канале
print(out.decode().strip(), proc.returncode) # → 42 0
asyncio.run(main())
See also
Open in the full glossarydocs.python.orgReport an inaccuracy