Frage

I find myself confronted with a gobject for the first time while trying to listening to dbus. I found this piece of code teaching me how to set up a main loop.

How can I listen for 'usb device inserted' events in Linux, in Python?

But what I need now is a way to stop it. The main loop has a quit function and I know how to call it from a keyboard interrupt exception. But how do I make it stop, when I put this main loop into a thread?

Or more general: What is the proper way to make this loop run independently, but also exit when the main function exits?

I appreciate your help.

War es hilfreich?

Lösung

Don't know anything about python but in general you should pass the loop variable to the calling thread (your main function) and then loop.quit() from there when you are done.

To pass the loop variable you have basically two options:

  1. the easy way: initialize loop with gobject.MainLoop() before forking, so it is available from both threads;
  2. the hard way: pass the value to the main thread after having initialized it in the loop thread.

An example (in C) of passing data between threads is shown by the GCond documentation (push_data() and pop_data()).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top