"""
ActiveState:
http://code.activestate.com/recipes/578634-pyqt-pyside-thread-safe-callbacks-main-loop-integr/
ref and proxy classes are based on:
http://code.activestate.com/recipes/81253/#c5
Modified proxy to support a one-way concept for callbacks
that will have no return value and can simply pass if they were
not valid, instead of raising an exception.
Callback event dispatch based on:
http://code.activestate.com/recipes/578299-pyqt-pyside-thread-safe-global-queue-main-loop-int/
Modified to simplify the process, by removing the Queue and threaded dispatcher,
and just using a more developed Event object that posts directly to the event loop.
"""
import weakref
import types
class ref(object):
"""
A weak method implementation
"""
def __init__(self, method):
try:
if method.im_self is not None:
# bound method
self._obj = weakref.ref(method.im_self)
else:
# unbound method
self._obj = None
self._func = method.im_func
self._class = method.im_class
except AttributeError:
# not a method
self._obj = None
self._func = method
self._class = None
def __call__(self):
"""
Return a new bound-method like the original, or the
original function if refers just to a function or unbound
method.
Returns None if the original object doesn't exist
"""
if self.is_dead():
return None
if self._obj is not None:
# we have an instance: return a bound method
return types.MethodType(self._func, self._obj(), self._class)
else:
# we don't have an instance: return just the function
return self._func
def is_dead(self):
"""
Returns True if the referenced callable was a bound method and
the instance no longer exists. Otherwise, return False.
"""
return self._obj is not None and self._obj() is None
def __eq__(self, other):
try:
return type(self) is type(other) and self() == other()
except:
return False
def __ne__(self, other):
return not self == other
#
# The modified proxy class, adding a oneWay option
#
class proxy(ref):
"""
Exactly like ref, but calling it will cause the referent method to
be called with the same arguments. If the referent's object no longer lives,
ReferenceError is raised.
If oneWay is True, then a ReferenceError is not raise and the callback
silently fails if it is no longer valid.
"""
def __init__(self, method, oneWay=False):
super(proxy, self).__init__(method)
self._oneWay = oneWay
def __call__(self, *args, **kwargs):
func = ref.__call__(self)
if func is None:
if self._oneWay:
return
else:
raise ReferenceError('object is dead')
else:
return func(*args, **kwargs)
def __eq__(self, other):
try:
func1 = ref.__call__(self)
func2 = ref.__call__(other)
return type(self) == type(other) and func1 == func2
except:
return False
#
# PyQt4 / PySide Thread-Safe Callback Dispatch
#
try:
from PySide import QtGui, QtCore
except ImportError:
from PyQt4 import QtGui, QtCore
class _Invoker(QtCore.QObject):
def customEvent(self, e):
e.callback()
class CallbackEvent(QtCore.QEvent):
"""
A custom QEvent that contains a callback reference
Also provides class methods for conveniently executing
arbitrary callback, to be dispatched to the event loop.
"""
EVENT_TYPE = QtCore.QEvent.Type(QtCore.QEvent.registerEventType())
__invoker = _Invoker()
def __init__(self, func, *args, **kwargs):
super(CallbackEvent, self).__init__(self.EVENT_TYPE)
self.func = func
self.args = args
self.kwargs = kwargs
def callback(self):
"""
Convenience method to run the callable.
Equivalent to:
self.func(*self.args, **self.kwargs)
"""
self.func(*self.args, **self.kwargs)
@classmethod
def post(cls, func, *args, **kwargs):
"""
Post a callable to run in the main thread
"""
cls.post_to(cls.__invoker, func, *args, **kwargs)
@classmethod
def post_to(cls, receiver, func, *args, **kwargs):
"""
Post a callable to be delivered to a specific
receiver as a CallbackEvent.
It is the responsibility of this receiver to
handle the event and choose to call the callback.
"""
# We can create a weak proxy reference to the
# callback so that if the object associated with
# a bound method is deleted, it won't call a dead method
reference = proxy(func, oneWay=True)
event = cls(reference, *args, **kwargs)
# post the event to the given receiver
QtGui.QApplication.postEvent(receiver, event)
#
# Usage Example
#
from threading import Event
class Gui(QtGui.QMainWindow):
def __init__(self):
super(Gui, self).__init__()
self.setCentralWidget(QtGui.QLabel("Running callbacks...", self))
self._thread = Worker(self)
self._thread.finished.connect(QtGui.qApp.quit)
self._thread.start()
def closeEvent(self, e):
self._thread.stop()
super(Gui, self).closeEvent(e)
def customEvent(self, event):
print "Running callback from Gui.customEvent()"
event.callback()
# Just some random worker
class Worker(QtCore.QThread):
def __init__(self, parent=None):
super(Worker, self).__init__(parent)
self.__quitting = Event()
def run(self):
for i in xrange(5):
msg = "#{0}: Hi".format(i)
# Instruct the say() method to run in the main thread,
# being delivered to the parent
CallbackEvent.post_to(self.parent(), self.say, msg)
self.msleep(200)
# Run the same callback, but without a specific receiver.
# This will just be run directly in an anonymous receiver.
CallbackEvent.post(self.say, msg)
self.msleep(200)
# And lets just have one happen from this worker thread too
self.say(msg)
self.msleep(200)
if self.__quitting.is_set():
break
print "All done!"
def stop(self):
self.__quitting.set()
self.wait()
def say(self, word):
mainThread = QtGui.qApp.thread()
isMainThread = mainThread == self.currentThread()
print word, "from", "main" if isMainThread else "worker", "thread"
if __name__ == "__main__":
app = QtGui.QApplication([])
gui = Gui()
gui.show()
app.exec_()
Diff to Previous Revision
--- revision 2 2013-08-09 22:50:03
+++ revision 3 2013-08-15 10:58:06
@@ -1,4 +1,7 @@
"""
+ActiveState:
+http://code.activestate.com/recipes/578634-pyqt-pyside-thread-safe-callbacks-main-loop-integr/
+
ref and proxy classes are based on:
http://code.activestate.com/recipes/81253/#c5
@@ -18,7 +21,9 @@
import types
class ref(object):
-
+ """
+ A weak method implementation
+ """
def __init__(self, method):
try:
@@ -39,51 +44,53 @@
self._func = method
self._class = None
-
def __call__(self):
- '''
+ """
Return a new bound-method like the original, or the
original function if refers just to a function or unbound
method.
Returns None if the original object doesn't exist
- '''
+ """
if self.is_dead():
return None
+
if self._obj is not None:
# we have an instance: return a bound method
return types.MethodType(self._func, self._obj(), self._class)
+
else:
# we don't have an instance: return just the function
return self._func
-
-
+
def is_dead(self):
- '''Returns True if the referenced callable was a bound method and
+ """
+ Returns True if the referenced callable was a bound method and
the instance no longer exists. Otherwise, return False.
- '''
+ """
return self._obj is not None and self._obj() is None
-
def __eq__(self, other):
try:
return type(self) is type(other) and self() == other()
except:
return False
-
-
+
def __ne__(self, other):
return not self == other
- #
- # The modified
- #
+#
+# The modified proxy class, adding a oneWay option
+#
class proxy(ref):
- '''
+ """
Exactly like ref, but calling it will cause the referent method to
be called with the same arguments. If the referent's object no longer lives,
ReferenceError is raised.
- '''
+
+ If oneWay is True, then a ReferenceError is not raise and the callback
+ silently fails if it is no longer valid.
+ """
def __init__(self, method, oneWay=False):
super(proxy, self).__init__(method)
@@ -100,8 +107,7 @@
else:
return func(*args, **kwargs)
-
-
+
def __eq__(self, other):
try:
func1 = ref.__call__(self)
@@ -121,8 +127,22 @@
from PyQt4 import QtGui, QtCore
+class _Invoker(QtCore.QObject):
+
+ def customEvent(self, e):
+ e.callback()
+
+
class CallbackEvent(QtCore.QEvent):
+ """
+ A custom QEvent that contains a callback reference
+
+ Also provides class methods for conveniently executing
+ arbitrary callback, to be dispatched to the event loop.
+ """
EVENT_TYPE = QtCore.QEvent.Type(QtCore.QEvent.registerEventType())
+
+ __invoker = _Invoker()
def __init__(self, func, *args, **kwargs):
super(CallbackEvent, self).__init__(self.EVENT_TYPE)
@@ -131,10 +151,30 @@
self.kwargs = kwargs
def callback(self):
+ """
+ Convenience method to run the callable.
+
+ Equivalent to:
+ self.func(*self.args, **self.kwargs)
+ """
self.func(*self.args, **self.kwargs)
@classmethod
- def post(cls, receiver, func, *args, **kwargs):
+ def post(cls, func, *args, **kwargs):
+ """
+ Post a callable to run in the main thread
+ """
+ cls.post_to(cls.__invoker, func, *args, **kwargs)
+
+ @classmethod
+ def post_to(cls, receiver, func, *args, **kwargs):
+ """
+ Post a callable to be delivered to a specific
+ receiver as a CallbackEvent.
+
+ It is the responsibility of this receiver to
+ handle the event and choose to call the callback.
+ """
# We can create a weak proxy reference to the
# callback so that if the object associated with
# a bound method is deleted, it won't call a dead method
@@ -149,20 +189,37 @@
#
# Usage Example
#
+
+from threading import Event
+
+
class Gui(QtGui.QMainWindow):
+
def __init__(self):
- QtGui.QMainWindow.__init__(self)
+ super(Gui, self).__init__()
+ self.setCentralWidget(QtGui.QLabel("Running callbacks...", self))
+
self._thread = Worker(self)
+ self._thread.finished.connect(QtGui.qApp.quit)
self._thread.start()
+ def closeEvent(self, e):
+ self._thread.stop()
+ super(Gui, self).closeEvent(e)
+
def customEvent(self, event):
+ print "Running callback from Gui.customEvent()"
event.callback()
# Just some random worker
class Worker(QtCore.QThread):
+ def __init__(self, parent=None):
+ super(Worker, self).__init__(parent)
+ self.__quitting = Event()
+
def run(self):
for i in xrange(5):
@@ -171,12 +228,26 @@
# Instruct the say() method to run in the main thread,
# being delivered to the parent
- CallbackEvent.post(self.parent(), self.say, msg)
- self.sleep(1)
+ CallbackEvent.post_to(self.parent(), self.say, msg)
+ self.msleep(200)
+
+ # Run the same callback, but without a specific receiver.
+ # This will just be run directly in an anonymous receiver.
+ CallbackEvent.post(self.say, msg)
+ self.msleep(200)
# And lets just have one happen from this worker thread too
self.say(msg)
- self.sleep(1)
+ self.msleep(200)
+
+ if self.__quitting.is_set():
+ break
+
+ print "All done!"
+
+ def stop(self):
+ self.__quitting.set()
+ self.wait()
def say(self, word):
mainThread = QtGui.qApp.thread()