How to Achieve Non-Blocking Execution of Subprocess.call in Python?

Barbara Streisand
Release: 2024-10-19 13:53:02
Original
777 people have browsed it

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

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

Additional Notes

  • When using "shell=True," remember to avoid using a list to pass in arguments, as this can lead to security vulnerabilities.
  • The provided MCVE (Minimal Complete Verifiable Example) demonstrates non-blocking subprocess calls using both "subprocess.Popen()" and "asyncio," showcasing the concept in a practical setting.

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!

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!