I'm using python-dbus and cherrypy to monitor USB devices and provide a REST service that will maintain status on the inserted USB devices. I have written and debugged these services independently, and they work as expected.
Now, I'm merging the services into a single application. My problem is: I cannot seem to get both services ( cherrypy and dbus ) to start together. One or the other blocks or goes out of scope, or doesn't get initialized.
I've tried encapsulating each in its own thread, and just call start on them. This has some bizarre issues.
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
When this code is run, the cherrypy code doesn't fully initialize. When a USB device is inserted, cherrypy continues to initialize ( as if the threads are linked somehow ), but doesn't work ( doesn't serve up data or even make connections on the port ) I've looked at cherrypys wiki pages but haven't found a way to startup cherrypy in such a way that it inits, and returns, so I can init the DBus stuff an be able to get this out the door.
My ultimate question is: Is there a way to get cherrypy to start and not block but continue working? I want to get rid of the threads in this example and init both cherrypy and dbus in the main thread.