[PyQt] Any way to run code before each QThread?

Chris Pezley chris at pezley.net
Mon Jul 16 13:30:16 BST 2018

If anyone wants to know what a monkey patch for that would look like 

     import weakref
     import site
     site._qthread_instances = weakref.WeakSet()

     import PyQt5.QtCore

     def __new__(cls, *args, **kwargs):
         obj = super(PyQt5.QtCore.QThread, cls).__new__(cls, *args, 
         return obj

     PyQt5.QtCore.QThread.__new__ = __new__

You then access the list through site. You just need to make sure that 
this loads before the user code, though. I guess since coverage has an 
entry-point Ned doesn't need to worry about that.

On 07/16/2018 01:57 PM, Florian Bruhin wrote:
> On Mon, Jul 16, 2018 at 01:52:38PM +0200, Chris Pezley wrote:
>> You can track all instances of a class in python by creating a cache which
>> is populated when __new__ is called:
>>      [...]
> You need to use your custom MyQThread subclass everywhere for it to work
> though - in the case of coverage.py, I'm guessing users don't want their
> code to change (and depend on coverage.py) just to measure coverage.
> (coverage.py could monkeypatch the QThread class like apparently Eric
> and Wing IDE do, but I assume that comes with some nasty gotchas)
> Florian

More information about the PyQt mailing list