2017-04-04 76 views
0

OS Python的多: - Mac OSX上
的Python没有锁定父进程

我是新与蟒蛇多处理。对于我的应用程序,我想从main打开一个新进程并运行它,而不锁定主进程。例如。我正在运行进程A,现在我需要从A打开一个新应用程序,我们称它为进程B.我想打开B,以便它不会阻止进程A,并且仍然从进程中我应该能够停止每当我希望的过程。

直到现在无论我试过的代码是基本的,它们锁定了过程A.因此我无法实现它。有没有解决方法可以做到这一点?

我读了关于fork和spawn,但无法理解如何使用它来打开应用程序。我也尝试过线程。但没有成功。谁能告诉我,我该怎么做?

目前我使用subprocess.call()通过Python打开Mac应用程序。 这将是非常有益的。

编辑: -

I tried the accepted answer of this link 但无济于事。因为它会阻止终端,一旦我们手动关闭应用程序,它将退出输出0.

此外我已经尝试过这种solution。它也会这样做。我想让调用进程不被被调用进程阻塞。

同时在windows中使用os.system()完成同样的任务时,给了我我想要的东西。但我不知道我怎么能在Mac上做到这一点。

编辑2:CODE

模块1:

import subprocess 


def openCmd(name): 

    subprocess.call(["/usr/bin/open", "-W", "-n", "-a", "/Applications/"+name+".app"]) 

def closeCmd(name): 
    subprocess.call(['osascript', '-e', 'tell "'+name+'" to quit']) 

主要模块:

import speech_recognition as sr 
import pyttsx 
import opCl 


speech_engine = pyttsx.init('nsss') 
speech_engine.setProperty('rate', 150) 

OPEN_COGNATES=['open'] 
CLOSE_COGNATES=['close'] 

def speak(text): 
    speech_engine.say(text) 
    speech_engine.runAndWait() 

re = sr.Recognizer() 

def listen(): 
    with sr.Microphone() as source: 
     re.adjust_for_ambient_noise(source) 


     while True: 
      speak("Say something!") 
      print ">>", 
      audio = re.listen(source) 

      try: 
       speak("now to recognise it,") 
       value=re.recognize_google(audio) 
       print (value) 
       speak("I heard you say {}".format(value)) 
       value=value.strip().split() 
       name=" ".join(value[1:]) 
       if value[0] in OPEN_COGNATES: 
        speak("opening "+name) 
        opCl.openCmd(name) 
        pass 
       elif value[0] in CLOSE_COGNATES: 
        speak("opening "+name) 
        opCl.closeCmd(name) 
        pass 
       else: 
        pass 


      except sr.UnknownValueError as e: 
       speak("Could not understand audio") 
       print ('Could not understand audio') 
      except sr.RequestError as e: 
       speak("can't recognise what you said.") 
       print ("can't recognise what you said") 


if __name__=='__main__': 
    listen() 
+0

@stovfl我编辑的问题。我希望现在它可以帮助。 –

+0

我希望现在可以运作@stovfl –

回答

0

Comment: it gave a traceback. FileNotFoundError: [Errno 2] No such file or directory: 'leafpad'

我写,我不能我们"/usr/bin/open"osascript,所以我的例子使用'leafpad'

您是否试过用
Popen(["/usr/bin/open", "-W", "-n", "-a", "/Applications/"+name+".app"])替换Popen([name])

command line开始时,您必须通过相同的command args
阅读:launch-an-app-on-os-x-with-command-line

Reread From Python » 3.6.1 Documentation subprocess.Popen
Note shlex.split() can be useful when determining the correct tokenization for args, especially in complex cases:


Python » 3.6.1 Documentation:
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)
Run the command described by args. Wait for command to complete, then return the returncode attribute

你给"/usr/bin/open"osascript没有为我工作。

From Python » 3.6.1 Documentation subprocess.Popen
NOWAIT例如,例如:

import subprocess 

def openCmd(name): 
    subprocess.Popen([name]) 

def closeCmd(name): 
    subprocess.Popen(['killall', name]) 

if __name__ == '__main__': 
    while True: 
     key = input('input 1=open, 0=cloes, q=quit:') 
     if key == '1': 
      openCmd(('leafpad')) 
     if key == '0': 
      closeCmd('leafpad') 
     if key == 'q': 
      break 

:杀灭process可能导致数据洛斯和或其他问题。

测试使用Python 3.4.2

+0

它给了回溯。 'FileNotFoundError:[Errno 2]没有这样的文件或目录:'leafpad'' 使用safari尝试的事件没有打开应用程序。我试图在python 3.6.1 'FileNotFoundError:[Errno 2]没有这样的文件或目录:'safari'' –

+0

Thanx男子。它现在有效。但是idk killall没有工作。相反'['osascript','-e','告诉应用程序''+ name +'“退出']'这个工作。非常感谢。 –