Calum
Calum

Reputation: 2130

how to create and destroy non blocking sub process that are OS independent in python?

What is the best of doing this?

Upvotes: 2

Views: 103

Answers (1)

Generic Ratzlaugh
Generic Ratzlaugh

Reputation: 747

As @Keith suggested use subprocess module, but more specifically use Popen. For example, on Windows, this opens myfile.txt with notepad and then terminates it after a 20 seconds:

import subprocess
import time

command = "notepad myfile.txt"
pipe = subprocess.Popen(command, shell=False)
time.sleep(5)
pipe.poll()
print("%s" % pipe.returncode)   #"None" when working fine
time.sleep(5)
pipe.terminate()
pipe.wait()
print("%s" % pipe.returncode)   # 1 after termination

Upvotes: 1

Related Questions