I am working on a GUI in python 3.5 with PyQt5 for a small chat bot. The problem i have is that the pre-processing, post-processing and brain are taking too much time to give back the answer for the user provided input.
The GUI is very simple and looks like this: http://prntscr.com/dsxa39 it loads very fast without connecting it to other modules. I mention that using sleep before receiving answer from brain module will still make it unresponsive.
self.conversationBox.append("You: "+self.textbox.toPlainText())
self.textbox.setText("")
time.sleep(20)
self.conversationBox.append("Chatbot: " + "message from chatbot")
this is a small sample of code, the one that i need to fix.
And this is the error I encounter: http://prnt.sc/dsxcqu
I mention that I've searched for the solution already and everywhere I've found what I've already tried, to use sleep. But again, this won't work as it makes the program unresponsive too.
Slow functions, such as
sleep
, will always block unless they are running asynchronously in another thread.If you want to avoid threads a workaround is to break up the slow function. In your case it might look like:
where
self.app
is a reference to your QApplication instance. This solution is a little hacky as it will simply result in 20 short hangs instead of one long hang.If you want to use this approach for your brain function then you'll need it to break it up in a similar manner. Beyond that you'll need to use a threaded approach.