Home >Backend Development >Python Tutorial >How to Achieve Non-Blocking Execution of Subprocess.call in Python?
Non-Blocking subprocess.call: Achieving Parallel Script Execution
When executing asubprocess.call() in a Python program, the caller typically waits until the subprocess completes before proceeding. However, for certain applications, it may be desirable to make the subprocess non-blocking, allowing the caller to continue execution while the subprocess runs concurrently.
Problem Scenario
Consider the following scenario: you wish to start a "slave.py" script as a non-blockingsubprocess.call() from your "main.py" program. You need to pass arguments from "main.py" to "slave.py" once when the latter is first started but do not require further communication between the scripts after that.
Solution: subprocess.Popen
To achieve non-blocking execution of "slave.py," replace "subprocess.call()" with "subprocess.Popen() in "main.py." Instead of waiting for the subprocess to complete, "subprocess.Popen()" returns immediately, allowing "main.py" to continue its operations.
Code Example
<code class="python">import subprocess import sys subprocess.Popen(["python", "slave.py"] + sys.argv[1:])</code>
Alternative: asyncio
For more recent versions of Python (3.5 or later), you can utilize the "asyncio" module to implement non-blocking subprocess calls. This approach leverages concurrency with coroutines to execute multiple tasks simultaneously.
Code Example (asyncio)
<code class="python">import asyncio async def do_subprocess(): proc = await asyncio.create_subprocess_exec('sleep', '5') returncode = await proc.wait() print(f'Subprocess done sleeping. Return code = {returncode}') loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.gather(do_subprocess())) loop.close()</code>
Additional Notes
The above is the detailed content of How to Achieve Non-Blocking Execution of Subprocess.call in Python?. For more information, please follow other related articles on the PHP Chinese website!