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:
subprocess.Popen(["python", "slave.py"] sys.argv[1:])
For a complete demonstration of non-blocking subprocess calls, consider the following code:
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)
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:
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()
This approach ensures that both the subprocess and the main program run concurrently, maximizing performance and responsiveness.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3