AWS Thinkbox Discussion Forums

Missing Attributes

Hello,
I apologize if this is the wrong place to post this. I don’t know where the problem is coming from and I’ve tried posting on the Qt forums, but still no answer.
I’m using QThreadPool and QRunnable to keep the user interface responsive, but I keep running into errors that happen inconsistently. I’ll post some example code to make things clearer.

Runnable.py

from PyQt5.QtCore import *
 
class RunnableSignals(QObject):
  finished = pyqtSignal(dict)
 
  def __init__(self):
    super(RunnableSignals, self).__init__()
 
class Runnable(QRunnable):
  def __init__(self, inputData):
    super(Runnable, self).__init__()
 
    self._inputData = inputData
    self.abort = False
    self.signals = RunnableSignals()
 
  def aborted(self):
    return self.abort
 
  def run(self):
    if self.aborted():
      return
 
  # Do stuff. Check if aborted along the way
 
  self.signals.finished.emit(data)

Test.py

class Test(QObject):
  def __init__(self):
    super(Test, self).__init__()

  def update(self):
    self.runnable = Runnable(someVariable)
    self.runnable.signals.finished.connect(self.onFinished)
    QThreadPool.globalInstance().start(self.runnable)

I keep getting the error AttributeError: ‘Runnable’ object has no attribute ‘abort’. How is this possible if it was declared in the constructor before the thread is even run? Also, it doesn’t error 100% of the time. Sometimes it works.
I don’t know if this has something to do with Qt, or if it’s PyQt, or if it’s Python .NET to blame.

Is your Runnable class interacting with the UI at all (ie: getting or setting values, updating the UI, etc)? If so, that could explain the strange behavior you’re seeing. Qt, like any UI libraries I’ve used in the past, requires you to only interact with the UI from the thread that created it (typically the main thread). If you launch a thread with the thread pool that interacts with the UI, it can have unexpected consequences and cause strange behavior.

We tend to use the QTimer class when we want to periodically update the UI. When the QTimer interval triggers, it does so on the main thread, so you can interact with the UI from it without any problems.

Cheers,
Ryan

Thank you for the reply. I make sure that all that I do in the Runnable object is just gather data and put it in a dict. I do interact with the UI in the slot that gets called by the finished signal. I tried adding Qt.QueuedConnection to the connect method, but that didn’t help. I became suspicious that Python might be trying to garbage collect when the thread pool was running, so I created a global Python variable for the thread pool like this: threadPool = QThreadPool.globalInstance(). Since I’ve done this, I haven’t had the problem happen. I’ll see if I can go a few days without it coming up, but I think the problem is gone.
Sorry. I thought it might have been a bug with one of Deadline’s frameworks, but I guess this was an error on my part.

Privacy | Site terms | Cookie preferences