Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to delete cache after using QWebEngineView



  • Hello,
    I have a python script that uses QWebEngineView, QWebEngineProfile and QWebEnginePage from PySide2 for displaying a customised web page. I have been looking for ways to clear the created cache at the time of program exit but have been unable to do much else than calling QWebEngineProfile.clearHttpCache() and QWebEngineProfile.clearAllVisitedLinks(). The trouble is that although they clear out the bulk of the QWebEngineView cache these functions still leaves some residual files on the PC.

    The QWebEngineProfile is created with a unique random name (generating unique random cache folders) to allow multiple instance of the python script running side by side without having the cache files being locked by the python program that was started first.

    I was hoping that it would be possible to clear the cache files using other python functions such as shutil.rmtree(cachepath) after I have deleted all references to QWebEngineView, QWebEngineProfile and QWebEnginePage. But that seems not to work. The cache files are locked, presumably by the QtWebEngineProcess which remains running for the lifetime of the python script and therefore results in an "access denied" exception whenever I try to delete them. Is there a recommended way to stop the QtWebEngineProcess to avoid this file locking?

    One workaround I have found is to invoke the QWebEngineProfile off-the-record (in private mode) so it doesn't write any cache files at all, let alone leave them on the PC. But I'm not sure this is the recommended way of cleaning up the after using the QWebEngineView. If anyone has some suggestions I'd be grateful to hear back.

    Many thanks,

    Rob



  • @Oeffner said in How to delete cache after using QWebEngineView:

    The cache files are locked, presumably by the QWebEngineProcess which remains running for the lifetime of the python script

    Do you mean that you have a QWebEngineView whose lifetime/scope covers the whole of your Python script, hence it will always be there, or do you mean you still see that process running once you have destroyed all QWebEngine stuff in your Python script which continues running?



  • @JonB , yes I still see the QtWebEngineProcess running despite of having tried to do away with all references to QWebEngineView, QWebEngineProfile and QWebEnginePage and explicit garbage colleting:

    del QWebEngineView
    del sys.modules["QWebEngineView"]
    import gc
    gc.collect()
    

    and similar for QWebEngineProfile and QWebEnginePage



  • @Oeffner
    Hmm.
    I had recent problem of code creating & getting rid of QWebEngineViews repeatedly consuming more & more memory/leaving 26 QtWebEngineProc processes running. I didn't look at temp files, but might be related. If your web engines don't properly disappear they are liable to hang onto their temp files.

    I am Ubuntu, but apparently my fix did work on Windows too. I use PyQt5 not PySide2. I had to use a PyQt call of sip(QObject obj).delete() to force it to be freed. I was told that compares to C++ delete. I don't know how your code actually compares to that. For example, if there's any reference to an instance around your code will accomplish nothing? You want to look carefully at your instances creation/destruction.. I found there was something about QWebEngineView which seemed to make it "stick around" more than other widgets, but never got to the bottom of that.

    One thing: does your code go back to the main Qt event loop after you have done whatever to delete your webengine? The issue was toed up with allowing tha\t to run to truly clear up.


Log in to reply