[Qgis-developer] Socket server freezes on Python plugin

Bernhard Ströbl bernhard.stroebl at jena.de
Tue Jun 19 02:34:57 PDT 2012


Hi Victor,

I happen to have done just that recently. I used QTcpServer, not sure if 
it is the best way to do it, but it works with QGIS 1.7.4 and 1.8. I 
think I used the PyQt examples. I hope it is of use.

from PyQt4 import QtCore, QtGui, QtSql, QtNetwork

class MyPlugin:
     def __init__(self, iface):
         self.startMyServer()
         app = QgsApplication.instance()
         QtCore.QObject.connect(app, QtCore.SIGNAL("aboutToQuit()"),
	self.stopListening)

     def stopListening(self):
         if self.tcpServer:
             self.tcpServer.close()

             if self.tcpSocket:
                 self.tcpSocket.close()

     def startMyServer(self):
         '''create the server'''

         self.tcpServer = QtNetwork.QTcpServer()
         self.tcpSocket = None # initialize

         if not self.tcpServer.listen(port=16500):
             QtGui.QMessageBox.critical(None, "Tcp Server",
                     "Could not start the server: %s." \
	    % self.tcpServer.errorString())
             return None

         QtCore.QObject.connect(self.tcpServer,
	QtCore.SIGNAL("newConnection()"), self.receiveMyData)

     def receiveMyData(self):
         '''slot to be called when a client connects to the server'''
         if self.tcpServer.hasPendingConnections():
             self.tcpSocket = self.tcpServer.nextPendingConnection()
             self.tcpSocket.readyRead.connect(self.readMyData)

     def readMyData(self):
         '''Slot to read the data sent to the server'''
         numbBytes = self.tcpSocket.bytesAvailable()
         data = self.tcpSocket.read(numbBytes)
         self.tcpSocket.close()

	# do something witht the data

Am 19.06.2012 11:18, schrieb Víctor González:
> Hi,
>
> I'm trying to create a plugin that opens a socket server in a separate
> thread and handles requests. I create the server using
> SocketServer.TCPServer and threading.Thread in the initGui method and
> the plugin starts correctly, but if I try to get a response from a
> client, the server freezes and does not respond.
>
> The strange thing is that I have added a toolbar button that opens and
> closes a file and, if I click that button, the server unfreezes and I
> get the response as expected. However, if I create the socket server
> with the same exact code as an standalone program, it works as expected
> without freezing.
>
> I have looked into the PyQgis cookbook [1] and the contributed plugins
> [2] searching for an example or solution but I couldn't find anything.
> Does anyone know what may be wrong? Any hint will be greatly
> appreciated. I attach the Qgis plugin as well as the standalone server
> and client.
>
> Thanks in advance,
> Víctor.
>
>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
>


________ Information from NOD32 ________
This message was checked by NOD32 Antivirus System for Linux Mail Server.
http://www.nod32.com


More information about the Qgis-developer mailing list