Handling unixSignal in PyQt in a console app
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.
Code:
import sys
import time
from PyQt4 import QtCore
HUP_EVENT
= QtCore.
QEvent.
User + 1
def __init__(self):
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
Quote:
In event, 67
Hangup
Which shows the unix signal was not trapped.
This shows PyQt trashes the python signal module handlers
Code:
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
))
def __init__(self):
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
and then nothing.
Comment out the instantiation of the app and...
Code:
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
))
def __init__(self):
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'
Quote:
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