2
  • In a python script i want to spawn a process that runs a file in the same directory
  • I dont want the python script to be blocked by the new process
  • Then want to be able to close the spawned process from the script.
  • Ontop of it all i need it to be OS independant.

What is the best of doing this?

4
  • Have you tried the subprocess module? Commented Jan 19, 2013 at 2:30
  • ive had a bit of a play round, maybe not enough. always seem to run into the issue of the process being blocking or not correctly getting the pid Commented Jan 19, 2013 at 2:43
  • Can you post what you've tried? Commented Jan 19, 2013 at 2:53
  • i cant recall all of what i tried off hand, as i was at another machine while trying it. mostly was playing round with the call and Popen methods Commented Jan 19, 2013 at 3:08

1 Answer 1

1

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 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.