如何处理流程?

维斯特斯

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

  • 所以首先我需要检查它是否正在运行。我看到了一个答案,您可以在程序启动时制作一个文件巫婆,然后检查它是否存在...但是,如果有人将其删除该怎么办?我是否可以仅问操作系统是否存在名为“ myApp”的进程?
  • 我真正不了解的下一件事是如何与流程进行沟通。我如何为它提供输入数据,这将如何处理?通过main()方法,它是否像启动新应用程序一样起作用?

我正在尝试使用Quickly创建此文件。因此,如果您可以给我一些python示例或链接到类似的东西,那将是很好的。

维斯特斯

我发现我确实需要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...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章