[PyKDE] Run function in the background

Tom Brown tom at nextstate.net
Mon Feb 19 18:57:13 GMT 2007


On Mon, 2007-02-19 at 19:11 +0100, Fabian Steiner wrote:
> Hello!
> 
> I am currently working on an application which controls several hosts in 
> our network in order to ensure that there are online. This is done by a 
> function called checkStatus() that needs to be called every 10 seconds. 
> I tried the following (with checkStatus being called in __init__ as 
> thread.start_new_thread(self.checkStatus, ()), but it did't work since 
> it resulted in a segmentation fault:
> 
> 
> [...]
> checkStatus(self):
>      while 1:
>             if check_online('192.168.0.100'):
>                 self.ui.lblStatus.setText('ON')
>                 self.ui.btnOn.setEnabled(False)
>                 self.ui.btnOff.setEnabled(True)
>             else:
>                 self.ui.lblStatus.setText('OFF')
>                 self.ui.btnOn.setEnabled(True)
>                 self.ui.btnOff.setEnabled(False)
>             time.sleep(10)
> 
> Output:
> fabi at einstein:~/dev/Server_Control$ python Server_Control.py
> QObject: Cannot create children for a parent that is in a different thread.
> QObject: Cannot create children for a parent that is in a different thread.
> Segmentation fault
> 
> Is there any other way which would solve this issue?

You cannot update UI elements directly from a thread. I believe you
should use qApp.postEvent(). You can post events to the main window
which you catch in customEvent(). You have to override customEvent() in
your main window class. You probably want to use a QThread instead of a
python thread. I had better luck using QThread in my production
application.

HTH,
Tom




More information about the PyQt mailing list