4

I found some articles and even stack|overflow questions addressing this subject, but I still can't do it..

What I want to do is open an instance of firefox from python. then the python application should keep minding its own business and ignore the firefox process.

I was able to achive this goal on Windows-7 and XP using:

subprocess.Popen()

On OS X I tried:

subprocess.Popen(['/Applications/Firefox.app/Contents/MacOS/firefox-bin'])
subprocess.call(['/Applications/Firefox.app/Contents/MacOS/firefox-bin'])
subprocess.call(['/Applications/Firefox.app/Contents/MacOS/firefox-bin'], shell=True)
os.system('/Applications/Firefox.app/Contents/MacOS/firefox-bin') 

(and probably some others I forgot) to no avail. My python app freezes till I go and close the firefox app.

What am I missing here? any clues?

Leeron
  • 147
  • 1
  • 2
  • 11
  • This previous answer works for me: http://stackoverflow.com/questions/832331/launch-a-webpage-on-a-firefox-win-tab-using-python/832338#832338 – Kris K. Jun 22 '11 at 14:49
  • Firefox is just an example in this case (that's why it's not in the title or tags). My process needs to be able to open any other process. – Leeron Jun 22 '11 at 14:55

2 Answers2

3

To show what I meant:

import os
if not os.fork():
    os.system('firefox')
os._exit(0)

Version that doesn't quit the main Python process:

import os
if not os.fork():
    os.system('firefox')
    os._exit(0)
Jasmijn
  • 9,370
  • 2
  • 29
  • 43
2

You'll need to detach the process somehow. I snatched this from spawning process from python

import os
pid = os.fork()
if 0 == pid:
  os.system('firefox')
  os._exit(0)
else:
  os._exit(0)

This spawns a forked headless version of the same script which can then execute whatever you like and quit directly afterwards.

Community
  • 1
  • 1
sapht
  • 2,789
  • 18
  • 16
  • 1
    You could simplify this a lot. Unfortunately, I can't really put it in this comment. – Jasmijn Jun 22 '11 at 14:59
  • I thought about going this route and even gave it a short try but couldn't get it to work. You're code make my main process exit.. – Leeron Jun 22 '11 at 15:10
  • 1
    @Leeron: When you say "my main process" are you referring to firefox or the python script? The `os._exit` calls quits the process, so if you're referring to your python script and you don't want it to exit immediately afterwards, just remove that one line. @Robin: Yes, you could, but would it make the sequence easier to understand? – sapht Jun 22 '11 at 15:20
  • Well what finely (or at the moment) worked was using fork and calling os.system then os._exit when in the child process (your code without the else block) Thanks – Leeron Jun 22 '11 at 15:59