Spawn a new non-blocking process using Python on Mac OS X

此生再无相见时 提交于 2019-12-10 16:37:01

问题


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?


回答1:


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.




回答2:


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)


来源:https://stackoverflow.com/questions/6441807/spawn-a-new-non-blocking-process-using-python-on-mac-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!