PDA

View Full Version : Handling unixSignal in PyQt in a console app



null
26th January 2009, 23:59
If you try to use the python standard unix signal handlers with QCoreApplication the handler never gets called. Comment out the creation(and exec_) call, the signal handler gets called as expected.

So, I'm forced to use the unixSignal qt SIGNAL of QCoreApplication but I can't get it to actually trap the SIGHUP.



import sys
import time
from PyQt4 import QtCore

HUP_EVENT = QtCore.QEvent.User + 1


class Application(QtCore.QCoreApplication):
def __init__(self):
QtCore.QCoreApplication.__init__(self, sys.argv)

self.connect(self, QtCore.SIGNAL('unixSignal(int)'), self.onUnixSignal)
self.connect(self, QtCore.SIGNAL('sigHUP()'), self.onSIGHUP)


def onUnixSignal(self, signum):
print 'unix signal'
if signum == signal.SIGHUP:
self.postEvent(self, QtCore.QEvent(HUP_EVENT))

def event(self, event):
print 'In event, %s' % event.type()
if event.type() == 1:
self.emit(QtCore.SIGNAL("sigHUP()"))
return True

return False

def onSIGHUP(self):
print 'in onsighup'
self.quit()


if __name__ == '__main__':
app = Application()
sys.exit(app.exec_())
print 'Done'


Prints



In event, 67
Hangup


Which shows the unix signal was not trapped.

This shows PyQt trashes the python signal module handlers


import signal
import sys
import time
from PyQt4 import QtCore

HUP_EVENT = QtCore.QEvent.User + 1


def sighup_handler(signum, frame):
print 'python signal handler'
self.postEvent(self, QtCore.QEvent(HUP_EVENT))



class Application(QtCore.QCoreApplication):
def __init__(self):
QtCore.QCoreApplication.__init__(self, sys.argv)

self.connect(self, QtCore.SIGNAL('sigHUP()'), self.onSIGHUP)


def event(self, event):
print 'In event, %s' % event.type()
if event.type() == HUP_EVENT:
self.emit(QtCore.SIGNAL("sigHUP()"))
return True

return False

def onSIGHUP(self):
print 'in onsighup'
self.quit()


if __name__ == '__main__':
app = Application()

signal.signal(signal.SIGHUP, sighup_handler)

sys.exit(app.exec_())

print 'Done'


Prints



In event, 67


and then nothing.

Comment out the instantiation of the app and...


import signal
import sys
import time
from PyQt4 import QtCore

HUP_EVENT = QtCore.QEvent.User + 1


def sighup_handler(signum, frame):
print 'python signal handler'
self.postEvent(self, QtCore.QEvent(HUP_EVENT))



class Application(QtCore.QCoreApplication):
def __init__(self):
QtCore.QCoreApplication.__init__(self, sys.argv)

self.connect(self, QtCore.SIGNAL('sigHUP()'), self.onSIGHUP)


def event(self, event):
print 'In event, %s' % event.type()
if event.type() == HUP_EVENT:
self.emit(QtCore.SIGNAL("sigHUP()"))
return True

return False

def onSIGHUP(self):
print 'in onsighup'
self.quit()


if __name__ == '__main__':
# app = Application()

signal.signal(signal.SIGHUP, sighup_handler)

# sys.exit(app.exec_())
time.sleep(30)

print 'Done'




python signal handler
Traceback (most recent call last):
File "sharedb/app.py", line 41, in <module>
time.sleep(30)
File "sharedb/app.py", line 11, in sighup_handler
self.postEvent(self, QtCore.QEvent(HUP_EVENT))
NameError: global name 'self' is not defined