How to Execute Non-Blocking Subprocesses in Python?

Susan Sarandon
Release: 2024-10-19 13:45:29
Original
484 people have browsed it

How to Execute Non-Blocking Subprocesses in Python?

Non-Blocking Subprocess Invocation

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.

Approach using subprocess.Popen

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>
Copy after login

Comprehensive Example

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>
Copy after login

This code executes the 'sleep' command asynchronously, periodically checking its status until it completes.

Alternative Asynchronous Approach

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>
Copy after login

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!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!