假设一个 python 脚本在后台运行,当我在我的计算机 (Linux) 上启动某个应用程序/软件时,我如何触发脚本内的函数。
我在谷歌上搜索了很多,但找不到解决方案!
提前谢谢你。
最佳答案
假设,您必须在外部进程“abc”启动时调用函数 callemnow。有多种方法可以做到这一点,但下面是一个示例代码,您可以轻松理解。
Note: ps -eaf | grep will give process details. The same command you can call from python script vi subprocess.
while True is just for the convenience. You have to substitute that statement as per your logic flow
from subprocess import Popen, PIPE
def externalprocess():
cmd = Popen(['ps', '-eaf'],stdout=PIPE)
(output, err) = cmd.communicate()
return True if "abs" in output else False
def callmenow()
'''
your logic
'''
if __name__ == '__main':
while True:
if externalprocess():
callmenow()
关于python - 当某个应用程序在 Python 中启动时如何触发函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58004600/