Python, thread and gobject

2024/10/18 13:31:04

I am writing a program by a framework using pygtk. The main program doing the following things:

  1. Create a watchdog thread to monitor some resource
  2. Create a client to receive data from socket
  3. call gobject.Mainloop()

but it seems after my program enter the Mainloop, the watchdog thread also won't run.

My workaround is to use gobject.timeout_add to run the monitor thing.

But why does creating another thread not work?

Here is my code:

import gobject
import time
from threading import Threadclass MonitorThread(Thread):def __init__(self):Thread.__init__(self)def run(self):print "Watchdog running..."time.sleep(10)def main():mainloop = gobject.MainLoop(is_running=True)def quit():mainloop.quit()def sigterm_cb():gobject.idle_add(quit)t = MonitorThread()t.start()print "Enter mainloop..."while mainloop.is_running():try:mainloop.run()except KeyboardInterrupt:quit()if __name__ == '__main__':main()

The program output only "Watchdog running...Enter mainloop..", then nothing. Seems thread never run after entering mainloop.

Answer

Can you post some code? It could be that you have problems with the Global Interpreter Lock.

Your problem solved by someone else :). I could copy-paste the article here, but in short gtk's c-threads clash with Python threads. You need to disable c-threads by calling gobject.threads_init() and all should be fine.

https://en.xdnf.cn/q/72938.html

Related Q&A

How to type annotate overrided methods in a subclass?

Say I already have a method with type annotations:class Shape:def area(self) -> float:raise NotImplementedErrorWhich I will then subclass multiple times:class Circle:def area(self) -> float:retur…

Import Error: No module named pytz after using easy_install

Today is my first day at Python and have been going through problems. One that I was working on was, "Write a short program which extracts the current date and time from the operating system and p…

Python catch exception pandas.errors.ParserError: Error tokenizing data. C error

I am facing problem with my malfunction csv input file whole reading and which i can deal with by adding "error_bad_lines=False" in my read_csv func to remove those.But i need to report these…

Nested tags in BeautifulSoup - Python

Ive looked at many examples on websites and on stackoverflow but I couldnt find a universal solution to my question. Im dealing with a really messy website and Id like to scrape some data. The markup l…

How do I check if a string is a negative number before passing it through int()?

Im trying to write something that checks if a string is a number or a negative. If its a number (positive or negative) it will passed through int(). Unfortunately isdigit() wont recognize it as a numbe…

openpyxl chage font size of title y_axis.title

I am currently struggling with changing the font of y axis title & the charts title itself.I have tried to create a font setting & applying it to the titles - with no luck what so ever. new_cha…

Combination of all possible cases of a string

I am trying to create a program to generate all possible capitalization cases of a string in python. For example, given abcedfghij, I want a program to generate: Abcdefghij ABcdef.. . . aBcdef.. . ABCD…

How to change download directory location path in Selenium using Chrome?

Im using Selenium in Python and Im trying to change the download path. But either this: prefs = {"download.default_directory": "C:\\Users\\personal\\Downloads\\exports"} options.add…

Keras, TensorFlow : TypeError: Cannot interpret feed_dict key as Tensor

I am trying to use keras fune-tuning to develop image classify applications. I deployed that application to a web server and the image classification is succeeded.However, when the application is used …

How to get matplotlib to place lines accurately?

By default, matplotlib plot can place lines very inaccurately.For example, see the placement of the left endpoint in the attached plot. Theres at least a whole pixel of air that shouldnt be there. In f…