0

OS:- Mac OSX
Python

I'm new to Multiprocessing with python. For my application, I want to open a new process from main and run it without locking the main process. for eg. I'm running process A and now i need to open a new application from A, lets call it process B. I want to open B in such a way that it does not blocks process A and still from Process i should be able to stop the process whenever i wish to.

Till now whatever code i have tried are basic and they lock the process A. and hence i'm unable to achieve it. Is there any workaround to do this ?

I read about fork and spawn but couldn't understand how can i use it to open an application. And i have tried threading also. But with no success. Can anyone tell me how can i do that ?

Currently I'm using subprocess.call() to open Mac Applications through Python. It would be really helpful.

EDIT :-

I tried the accepted answer of this link but to no avail. Because it would block the terminal and once we close the app manually it exits with output 0.

Also I have tried this solution. It would do the same. I want to make the the calling process not to be blocked by the called process.

while doing the same task in windows with os.system() gives me exactly what i want. But i don't know how can i do this in Mac.

EDIT 2: CODE

module 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'])

main module:

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()
Community
  • 1
  • 1
Krishna Kumar
  • 17
  • 1
  • 8

1 Answers1

0

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

As i wrote, I can't us "/usr/bin/open" and osascript, so my example uses 'leafpad'.

Have you tried replacing Popen([name]) with your
Popen(["/usr/bin/open", "-W", "-n", "-a", "/Applications/"+name+".app"])?

You must pass the same command args as you start it from the command line.
Read this: 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

Your given "/usr/bin/open" and osascript didn't work for me.

From Python » 3.6.1 Documentation subprocess.Popen
NOWAIT example, for instance:

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

Note: Killing a process can lead to data loos and or other problems.

Tested with Python:3.4.2
I'm

Community
  • 1
  • 1
stovfl
  • 14,998
  • 7
  • 24
  • 51
  • it gave a traceback. `FileNotFoundError: [Errno 2] No such file or directory: 'leafpad'` event trying with safari didn't opened the application. i'm trying on python 3.6.1 `FileNotFoundError: [Errno 2] No such file or directory: 'safari'` – Krishna Kumar Apr 08 '17 at 06:37
  • Thanx man. It worked now. But idk killall didn't worked. Instead `['osascript', '-e', 'tell application"'+name+'" to quit']` this worked. Thank you so much. – Krishna Kumar Apr 09 '17 at 06:24