2012-05-30 2 views
0

작업을 수행하기 위해 외부 쉘 프로그램 (grep)을 실행하려는 dbus 서버를 작성하려고합니다.python : 간단한 dbus 예제 - os.fork() 서비스 루틴에서?

내가 할 때

프롬프트 $의 server.py 다음

:

프롬프트 $의 client.py 번호 즉, 잘 작동합니다. 자식 프로세스에서 grep 명령을 실행합니다.

프롬프트 $의 client.py 번호 ...하지만, 두 번째 호출은 다음과 같은 오류 메시지를 생성합니다

DBusException : org.freedesktop.DBus.Error.ServiceUnknown : 이름의 org.example.ExampleService 어떤에서 제공하지되었다 .service 파일

나는 붙어있다. 너 나를 도울 수 있니? 지금 client.py

import gtk, glib 
import os 
import dbus 
import dbus.service 
import dbus.mainloop.glib 
import subprocess 

messages_queue=list() 
grep_pid=0 


def queue_msg(message): 
    global messages_queue 
    messages_queue.append(message) 
    return 

def dequeue_msg(): 
    global messages_queue,grep_pid 
    if grep_pid != 0: 
     try: 
      pid=os.waitpid(grep_pid,os.P_NOWAIT) 
     except: 
      return True 
     if pid[0] == 0: 
      return True 
     grep_pid=0 

    if len(messages_queue) == 0: 
      return True 
    else: 
      tekst=messages_queue.pop(0) 

     cmd="grep 'pp'" 

     print cmd 
     #works fine, when I do return here 
     #return True 

    grep_pid=os.fork() 
    if grep_pid != 0: 
     return True 
    os.setpgid(0,0) 
    pop=subprocess.Popen(cmd,shell=True,stdin=subprocess.PIPE) 
    pop.stdin.write(tekst) 
    pop.stdin.close() 
    pop.wait() 
    exit(0) 

class DemoException(dbus.DBusException): 
    _dbus_error_name = 'org.example.Exception' 

class MyServer(dbus.service.Object): 

    @dbus.service.method("org.example.ExampleInterface", 
         in_signature='', out_signature='') 
    def QueueMsg(self): 
      queue_msg("ppppp") 

    @dbus.service.method("org.example.ExampleInterface", 
         in_signature='', out_signature='') 
    def Exit(self): 
     mainloop.quit() 

from dbus.mainloop.glib import threads_init 

if __name__ == '__main__': 
     glib.threads_init() 

     threads_init() 

     dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 

     session_bus = dbus.SessionBus() 
     name = dbus.service.BusName("org.example.ExampleService", session_bus) 
     object = MyServer(session_bus, '/My') 

     glib.timeout_add_seconds(1, dequeue_msg) 
     mainloop = glib.MainLoop() 
     print "Running example service." 
     mainloop.run() 

: 당신이 서비스에 액세스 할 때

import sys 
from traceback import print_exc 
import dbus 

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

    try: 
     remote_object = bus.get_object("org.example.ExampleService", 
             "/My") 

    except dbus.DBusException: 
     print_exc() 
     sys.exit(1) 

    iface = dbus.Interface(remote_object, "org.example.ExampleInterface") 

    iface.QueueMsg() 

    if sys.argv[1:] == ['--exit-service']: 
     iface.Exit() 

if __name__ == '__main__': 
    main() 
+1

저는 dbus-python으로 응용 프로그램을 통합하는 방법을 연구 중이며 귀하의 게시물을 발견했습니다. 메인 루프 반복 커스터마이징 (Customizing the main loop iteration)을 약간 아래로 스크롤하면이 [link] (http://developer.gnome.org/glib/2.31/glib-The-Main-Event-Loop.html)에 섹션이 있습니다. '. 그것은 glib mainloop을 커스터마이징하는 것에 관한 것이다;) 그리고 어떻게 * nix 시스템에서'fork'와 호환되지 않는지. – tijko

답변

0

당신은 일반적으로이 오류 메시지가 여기

는 server.py (이후 client.py)입니다 더 이상 사용할 수 없습니다. 서버가 아직 실행 중인지 확인하십시오.

d-feet을 사용하여 dbus 연결을 디버그 할 수 있습니다.

0

누락 된 .service 파일에 대한 오류 메시지는 dbus-1/services에서 서비스 파일을 만들어야 함을 의미합니다.

# /usr/local/share/dbus-1/services/org.example.ExampleService.service 
[D-BUS Service] 
Name=org.example.ExampleService 
Exec=/home/user1401567/service.py 

적어도 우분투 12.04에이 상세하게 (? 파일이 필요 할 때 사용하지 않았다 어쩌면 .service)하지만 포함되지 않는 자습서의 많은, DBUS 서비스 할 수있는 ': 예를 들어

그것없이 연결되어 있어야합니다.

+0

자습서 리소스를 추가 할 수 있습니까? 나는 웹 검색 (내 나쁜 추측)을 통해 그것을 찾을 수 없습니다. 이 .service 파일이 없어도 나를 위해 일했지만 이전에는 갑자기 작동을 멈추었습니다. 이유는 확실하지 않습니다. (또는 더 나은, 나에게 맞는 검색 키워드를 제공한다. 그래서 다음 번에 나는 더 많은 성공을 찾을 것이다 :)?) – n611x007