Main Dbus / GLib, Background Thread

I am starting to work with DBus and event-based programming in general. The service I'm trying to create really has three parts, but two are really server-side things.

1) The actual DBus server talks to the remote website via HTTPS, manages sessions, and transfers information to clients.

2) Another part of the service calls up a persistent save page every 2 minutes to keep the session active on the external website.

3) Customers make calls to the service to extract information from the service.

I found some simple examples of programs. I am trying to adapt them to prototype No. 1 and No. 2. Instead of creating separate programs for both. I thought I could run them in a single, dual-processor process.

The problem I am seeing is that I am calling time.sleep (X) on my supported thread. The thread goes to sleep, but never wakes up. I think GIL is not released by the main GLib loop.

Here is my theme code:

class Keepalive(threading.Thread): def __init__(self, interval=60): super(Keepalive, self).__init__() self.interval = interval bus = dbus.SessionBus() self.remote = bus.get_object("com.example.SampleService", "/SomeObject") def run(self): while True: print('sleep %i' % self.interval) time.sleep(self.interval) print('sleep done') reply_status = self.remote.keepalive() if reply_status: print('Keepalive: Success') else: print('Keepalive: Failure') 

From press statements, I know that a dream begins, but I never see a "dream."

Here is the main code:

 if __name__ == '__main__': try: dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) session_bus = dbus.SessionBus() name = dbus.service.BusName("com.example.SampleService", session_bus) object = SomeObject(session_bus, '/SomeObject') mainloop = gobject.MainLoop() ka = Keepalive(15) ka.start() print('Begin main loop') mainloop.run() except Exception as e: print(e) finally: ka.join() 

Some other observations:

I see the message "begin main loop", so I know that it gets control. Then I see "sleep% i", and after that nothing.

If I ^ C, then I see a "dream". After ~ 20 seconds, I get an exception from self.run () that the remote application did not respond:

DBusException: org.freedesktop.DBus.Error.NoReply: did not receive a response. Possible reasons include: the remote application did not send a response, the message bus security policy blocked the response, the response timed out, or the network connection was interrupted.

What is the best way to run my code on a server?

Thanks,

+6
source share
1 answer

You must explicitly enable multithreading when using gobject by calling gobject.threads_init() . See the PyGTK FAQ for more information.

Next to what you describe, timeouts seem to be better suited. Use the following:

 # Enable timer self.timer = gobject.timeout_add(time_in_ms, self.remote.keepalive) # Disable timer gobject.source_remove(self.timer) 

This calls the keepalive function every time_in_ms (milli) seconds. More information can be found in the PyGTK link .

+6
source

Source: https://habr.com/ru/post/899131/


All Articles