Home >Backend Development >Python Tutorial >How to Execute Non-Blocking Subprocesses in Python?
When executing external scripts using subprocess.call, maintaining a non-blocking workflow is essential to avoid stalling the main program. This article presents a comprehensive solution to achieve this objective.
The primary method to execute a non-blocking subprocess is to employ subprocess.Popen instead of subprocess.call. This alternative doesn't block the main program, allowing it to continue its operations while the subprocess runs independently. Here's an example:
<code class="python">subprocess.Popen(["python", "slave.py"] + sys.argv[1:])</code>
For a complete demonstration of non-blocking subprocess calls, consider the following code:
<code class="python">import subprocess import time p = subprocess.Popen(['sleep', '5']) while p.poll() is None: print('Still sleeping') time.sleep(1) print('Not sleeping any longer. Exited with returncode %d' % p.returncode)</code>
This code executes the 'sleep' command asynchronously, periodically checking its status until it completes.
For Python versions 3.5 and above, a more modern and efficient approach involves using asyncio. It allows for true concurrency, enabling multiple tasks to execute simultaneously. Here's an example:
<code class="python">import asyncio async def do_subprocess(): print('Subprocess sleeping') proc = await asyncio.create_subprocess_exec('sleep', '5') returncode = await proc.wait() print('Subprocess done sleeping. Return code = %d' % returncode) async def sleep_report(number): for i in range(number + 1): print('Slept for %d seconds' % i) await asyncio.sleep(1) loop = asyncio.get_event_loop() tasks = [ asyncio.ensure_future(do_subprocess()), asyncio.ensure_future(sleep_report(5)), ] loop.run_until_complete(asyncio.gather(*tasks)) loop.close()</code>
This approach ensures that both the subprocess and the main program run concurrently, maximizing performance and responsiveness.
The above is the detailed content of How to Execute Non-Blocking Subprocesses in Python?. For more information, please follow other related articles on the PHP Chinese website!