<div dir="ltr">Forgot to mention that some time ago I wrote a python class to handle all this boring stuff for QGIS 2: <br><br><a href="https://github.com/boundlessgeo/lib-qgis-commons/blob/master/qgiscommons2/network/networkaccessmanager.py">https://github.com/boundlessgeo/lib-qgis-commons/blob/master/qgiscommons2/network/networkaccessmanager.py</a><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 15, 2018 at 2:48 PM, Alessandro Pasotti <span dir="ltr"><<a href="mailto:apasotti@gmail.com" target="_blank">apasotti@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div class="h5">On Mon, Jan 15, 2018 at 12:48 PM, David Marteau <span dir="ltr"><<a href="mailto:dmarteau@3liz.com" target="_blank">dmarteau@3liz.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Hi,<div><br></div><div>We hit a very nasty problem when trying to load projects with wms layer defined from python: loading capabilities fail with timeout error, thus preventing creating any project or layer from python.</div><div><br></div><div>The problem has been verified in Debian, Ubuntu and fresh OSX build from master branch.</div><div><br></div><div>We have somehow been able to reduce the problem to the loading of the wmsprovider. The following python code provide a minimal example for reproducing the problem:</div><div><br></div><div>===================8<=========<wbr>=====================</div><div><div>import os</div><div>import logging</div><div><br></div><div>from qgis.core import QgsApplication, QgsMessageLog</div><div>from qgis.core import QgsProject, QgsProviderRegistry</div><div><br></div><div>logger = logging.getLogger()</div><div>logger.setLevel(logging.DEBUG)</div><div><br></div><div># No Display</div><div>os.environ['QT_QPA_PLATFORM'] = 'offscreen'</div><div><br></div><div>qgis_application = QgsApplication([], False )</div><div>qgis_application.setPrefixPath<wbr>('/usr/local', True)</div><div>qgis_application.initQgis()</div><div><br></div><div># Log any messages</div><div>def writelogmessage(message, tag, level):</div><div>    arg = '{}: {}'.format( tag, message )</div><div>    if level == QgsMessageLog.WARNING:</div><div>        logger.warning(arg)</div><div>    elif level == QgsMessageLog.CRITICAL:</div><div>        logger.error(arg)</div><div>    else:</div><div>        <a href="http://logger.info" target="_blank">logger.info</a>(arg)</div><div><br></div><div>messageLog = QgsApplication.messageLog()</div><div>messageLog.messageReceived.con<wbr>nect( writelogmessage )</div><div><br></div><div><br></div><div>wmsuri = ("contextualWMSLegend=0&crs=EP<wbr>SG:4326&dpiMode=7&featureCount<wbr>=10&format=image/jpeg"</div><div>          "&layers=s2cloudless&styles&am<wbr>p;tileMatrixSet=s2cloudless-wm<wbr>sc-14"</div><div>          "&url=<a href="http://tiles.maps.eox.at/wms?" target="_blank">http://tiles.maps.eox.at<wbr>/wms?</a>" )</div><div><br></div><div># Request Timeout</div><div>provider = QgsProviderRegistry.instance()<wbr>.createProvider( "wms", wmsuri )</div><div><br></div><div>qgis_application.exitQgis()</div><div>del qgis_application</div></div><div>====================8<========<wbr>=====================</div><div><br></div><div>This fail with the following qgis errors</div><div><br></div><div><div style="word-wrap:break-word">WMS: Download of capabilities failed: Operation canceled</div><div style="word-wrap:break-word">Network: Network request <a href="https://tiles.maps.eox.at/wms?SERVICE=WMS&REQUEST=GetCapabilities" target="_blank">https://tiles.maps.eox<wbr>.at/wms?SERVICE=WMS&REQUEST=<wbr>GetCapabilities</a> timed out</div></div><div><br></div><div>Note that the following piece of code work perfectly and proceed the request without problems:</div><div><br></div><div>==============================<wbr>==============================<wbr>==</div><div>from PyQt5.QtCore import QUrl, QEventLoop</div><div>from PyQt5.QtNetwork import QNetworkRequest</div><div><br></div><div>url = QUrl("<a href="https://tiles.maps.eox.at/wms?SERVICE=WMS&REQUEST=GetCapabilities" target="_blank">https://tiles.maps.eox.a<wbr>t/wms?SERVICE=WMS&REQUEST=GetC<wbr>apabilities</a>")</div><div><br></div><div>request = QNetworkRequest()</div><div>request.setUrl(url)</div><div>manager = QgsNetworkAccessManager.instan<wbr>ce()</div><div>replyObject = manager.get(request)</div><div><br></div><div>loop = QEventLoop()</div><div>def onfinish(  ):</div><div>    print("Qt Return Code:",replyObject.attribute(Q<wbr>NetworkRequest.HttpStatusCodeA<wbr>ttribute))</div><div>    answer = replyObject.readAll();</div><div>    print(answer[:200],"\n")</div><div>    loop.quit()</div><div><br></div><div>replyObject.finished.connect( onfinish )</div><div>loop.exec()</div><div>==============================<wbr>==============================<wbr>==</div><div><br></div><div>So this is not fundamentally a problem with the network manager.</div><div><br></div><div>At this point we are stuck because the provider do not do something fundamentally different from the last piece of code, </div><div><br></div><div>In qgis code the request execution stall at: <a href="https://github.com/qgis/QGIS/blob/master/src/providers/wms/qgswmscapabilities.cpp#L1964" target="_blank">https://github.com/qgis/QG<wbr>IS/blob/master/src/providers/<wbr>wms/qgswmscapabilities.cpp#<wbr>L1964</a></div><div> </div><div>The problem does not occur if we run the project from the Desktop application: we think that there is a problem with the QEventLoop execution but we have no clue atm to check this assumption.</div><div><br></div><div>So if anybody have any idea on the subject…...</div></div></blockquote><div><br></div><div><br></div></div></div><div>Hi David, <br></div><div><br></div><div>You  need an event loop to use a Q(qg)NetworkAccessManager, it's asynchronous.<span class="HOEnZb"><font color="#888888"><br></font></span></div><span class="HOEnZb"><font color="#888888"><div><br></div><br clear="all"></font></span></div><span class="HOEnZb"><font color="#888888"><br>-- <br><div class="m_6835474922810840141gmail_signature" data-smartmail="gmail_signature">Alessandro Pasotti<br>w3:   <a href="http://www.itopen.it" target="_blank">www.itopen.it</a></div>
</font></span></div></div>
</blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Alessandro Pasotti<br>w3:   <a href="http://www.itopen.it" target="_blank">www.itopen.it</a></div>
</div>