我正在使用python-dbus和cherrypy監視USB設備並提供REST服務,以便在插入的USB設備上保持狀態。我已經獨立編寫並調試了這些服務,並且按預期工作。DBus-Cherrypy合併問題
現在,我正在將這些服務合併到一個應用程序中。我的問題是: 我似乎無法讓兩個服務(cherrypy和dbus)一起啓動。其中一個或另一個阻止或超出範圍,或未被初始化。
我試過在每個線程中封裝每個線程,然後調用它們的開始。這有一些奇怪的問題。
class RESTThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
cherrypy.config.update({ 'server.socket_host': HVR_Common.DBUS_SERVER_ADDR, 'server.socket_port': HVR_Common.DBUS_SERVER_PORT, })
cherrypy.quickstart(USBRest())
class DBUSThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
DBusGMainLoop(set_as_default=True)
loop = gobject.MainLoop()
DeviceAddedListener()
print 'Starting DBus'
loop.run()
print 'DBus Python Started'
if __name__ == '__main__':
# Start up REST
print 'Starting REST'
rs = RESTThread()
rs.start()
db = DBUSThread()
db.start()
#cherrypy.config.update({ 'server.socket_host': HVR_Common.DBUS_SERVER_ADDR, 'server.socket_port': HVR_Common.DBUS_SERVER_PORT, })
#cherrypy.quickstart(USBRest())
while True:
x = 1
當這個代碼運行時,CherryPy的代碼不會完全初始化。當插入USB設備時,cherrypy會繼續初始化(好像線程以某種方式鏈接),但不起作用(不會提供數據甚至在端口上建立連接)我已經查看了cherrypys wiki頁面,但還沒有找到一種啓動cherrypy的方式,以便它能夠進入並返回,所以我可以啓動DBus的東西,使其能夠將其解鎖。
我的最終問題是:有沒有辦法讓櫻桃開始,不阻止,但繼續工作?我想擺脫這個例子中的線程,並在主線程中初始化cherrypy和dbus。
我試過這個: http://stackoverflow.com/questions/510821/how-to-write-a-functional-test-for-a-dbus-service-written-in-python/762079#762079 現在cherrypy工作得很好,但loop.run()似乎沒有工作。 – Therealstubot