[PyQt] PyQt5 QtWebEngine: can't block python till javascript is finished with a callback function

Phil Thompson phil at riverbankcomputing.com
Fri May 6 15:37:52 BST 2016


On 6 May 2016, at 10:28 am, Daan V. <daanv1987 at gmail.com> wrote:
> 
> When I call the runJavaScript function of QWebEnginePage with a callback I can't block the python code on this call. I've tried sleeping, "spinning the event loop" (Kovid Goyal - https://riverbankcomputing.com/pipermail/pyqt/2015-January/035324.html) and a combination of both. The example below shows how the "call_js_with_callback" keeps waiting.
> 
> When this while loop is removed from "call_js_with_callback" the callback actually happens. This means in an asynchronous environment this would not be an issue, but when porting a legacy project which was not written with asynchronous behavior in mind there is no quick way to port. Can anyone tell me if this is correct or if I've made a mistake and/or there is a workaround I can use?

With two changes I can get the code working...

html_ready() is being called from JavaScript and you are then calling back into JavaScript. I can imagine that might cause problems. Invoke call_js_with_callback() like this...

    QTimer.singleShot(0, self.call_js_with_callback)

You are filtering too many event types in your loop. Call processEvents() without any arguments.

> Another interesting thing is that a callback method declared as I have it will crash unless the @pyqtSlot() is changed in an @pyqtSlot(str) (or something similar I assume). With @pyqtSlot() the callback will retrieve None as parameter and a silent crash of the application happens. Maybe this can be added to the http://pyqt.sourceforge.net/Docs/PyQt5/api/qwebenginepage.html page or an error message can be returned when the callback fails.

I can't reproduce any crashes but there is no need to decorate the method at all.

Phil



More information about the PyQt mailing list