작업을 수행하기 위해 외부 쉘 프로그램 (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()
저는 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