如何使用流程?

Vie*_*urs 5 python dbus application-development

我在这里看到了类似的问题,但我没有得到答案。也许是因为我对这一切都很陌生,只是不明白。我希望我的应用程序主要用作指标。如果用户再次启动它,它会检查它是否已经在运行,如果它已经运行,则将所有输入数据提供给该进程并退出。

  • 所以首先我需要检查它是否正在运行。我看到了一个答案,你可以在程序启动时创建一个文件女巫,然后检查它是否存在......但是如果有人会删除它呢?我不能只问操作系统是否有名为“myApp”的进程?
  • 接下来我真正不明白的是如何与流程进行沟通。我如何给它输入数据,它会用它做什么?它是否像通过 main() 方法启动一个新应用程序一样工作?

我正在尝试使用 Quickly 创建它。所以如果你能给我一些 python 示例或链接到类似的东西,那就太好了。

Vie*_*urs 1

我发现我确实需要 DBus 来实现我所需要的。所以这就是我实际需要做的:

  1. 检查我的服务是否在 dbus 上
  2. 如果它将我所有的输入变量传递给它并退出
  3. 如果它没有创建我的 dbus 服务并
    在 Python 中启动我的程序,它看起来像这样:


# using quickly...  
#     
#   __init__.py  
# # # # # # # # # # # # #  
import dbus  
import sys  
from gi.repository import Gtk  
# import whatever else you need...  

from my_app import MyAppDBusService
# import whatever else from your app...

def main():
    bus = dbus.SessionBus()

    # Check if my app is running and providing DBus service
    if bus.name_has_owner('com.example.myApp'):
        #if it is running pass the commandline variables to it and exit

        #get my service from DBus
        myService = bus.get_object('com.example.myApp', '/com/example/myApp')
        #get offered method from DBus
        myMethod = myService.get_dbus_method('my_method', 'com.example.myApp')
        #call the method and pass comandline varialbes
        myMethod(sys.argv)
        #exit
        sys.exit(0)
    else:
        #if not running
        #run my DBus service by creating MyAppDBusService instance
        MyAppDBusService.MyAppDBusService()

        #do whatever with sys.argv...
        #...

        Gtk.main()

# MyAppDBusService.py
# # # # # # # # # # # # # #

import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
#import whatever else you need...

# use the dbus mainloop
DBusGMainLoop(set_as_default = True)

class MyAppDBusService(dbus.service.Object):
    def __init__(self):
        # create dbus service in the SessionBus()
        dbus_name = dbus.service.BusName('com.example.myApp', bus=dbus.SessionBus())
        dbus.service.Object.__init__(self, dbus_name, '/com/example/myApp')

    # offer a method to call using my dbus service
    @dbus.service.method('com.example.myApp')
    def my_method(self, argv):
        #do whatever with argv...
Run Code Online (Sandbox Code Playgroud)