Python 不锁定父进程的多处理
Python multiprocessing without locking the parent process
OS:- Mac OSX
Python
我是 python 的多处理新手。对于我的应用程序,我想从 main 和 运行 打开一个新进程而不锁定主进程。例如。我正在 运行ning 进程 A,现在我需要从 A 打开一个新的应用程序,我们称之为进程 B。我想以这样一种方式打开 B,它不会阻止进程 A 并且仍然来自进程 i应该可以随时停止进程。
到目前为止,我尝试过的任何代码都是基本的,它们会锁定进程 A。因此我无法实现它。有什么解决方法可以做到这一点吗?
我阅读了有关 fork 和 spawn 的内容,但不明白如何使用它来打开应用程序。我也试过穿线。但没有成功。谁能告诉我该怎么做?
目前我正在使用 subprocess.call() 通过 Python 打开 Mac 应用程序。
这真的很有帮助。
编辑:-
I tried the accepted answer of this link
但无济于事。因为它会阻塞终端,一旦我们手动关闭应用程序,它就会退出并输出 0。
我也试过这个solution。它会做同样的事情。我想让调用进程不被被调用进程阻塞。
在 windows 中使用 os.system() 执行相同的任务时,我得到了我想要的结果。但我不知道如何在 Mac 中执行此操作。
编辑 2:代码
模块 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()
Comment: it gave a traceback. FileNotFoundError: [Errno 2] No such file or directory: 'leafpad'
如我所写,我不能我们 "/usr/bin/open"
和 osascript
,所以我的示例使用 'leafpad'
.
您是否尝试过用您的
替换Popen([name])
Popen(["/usr/bin/open", "-W", "-n", "-a", "/Applications/"+name+".app"])
?
您必须传递与从 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
我
OS:- Mac OSX
Python
我是 python 的多处理新手。对于我的应用程序,我想从 main 和 运行 打开一个新进程而不锁定主进程。例如。我正在 运行ning 进程 A,现在我需要从 A 打开一个新的应用程序,我们称之为进程 B。我想以这样一种方式打开 B,它不会阻止进程 A 并且仍然来自进程 i应该可以随时停止进程。
到目前为止,我尝试过的任何代码都是基本的,它们会锁定进程 A。因此我无法实现它。有什么解决方法可以做到这一点吗?
我阅读了有关 fork 和 spawn 的内容,但不明白如何使用它来打开应用程序。我也试过穿线。但没有成功。谁能告诉我该怎么做?
目前我正在使用 subprocess.call() 通过 Python 打开 Mac 应用程序。 这真的很有帮助。
编辑:-
I tried the accepted answer of this link 但无济于事。因为它会阻塞终端,一旦我们手动关闭应用程序,它就会退出并输出 0。
我也试过这个solution。它会做同样的事情。我想让调用进程不被被调用进程阻塞。
在 windows 中使用 os.system() 执行相同的任务时,我得到了我想要的结果。但我不知道如何在 Mac 中执行此操作。
编辑 2:代码
模块 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()
Comment: it gave a traceback. FileNotFoundError: [Errno 2] No such file or directory: 'leafpad'
如我所写,我不能我们 "/usr/bin/open"
和 osascript
,所以我的示例使用 'leafpad'
.
您是否尝试过用您的
替换Popen([name])
Popen(["/usr/bin/open", "-W", "-n", "-a", "/Applications/"+name+".app"])
?
您必须传递与从 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
我