[PyQt] two classes in same event loop
Steve Castellotti
sc at puzzlebox.info
Fri Jul 9 01:34:55 BST 2010
Thanks, that's a good idea, and I've tried it, but still no luck.
Its very strange, I've added some debugging code, so on the client
side it looks like this:
if self.socket.waitForConnected(5000):
print "client: writing"
self.socket.write(data)
print "client: sent"
And the server side looks like:
...
self.socket.newConnection.connect(self.processConnection)
...
def processConnection(self):
print "server: connected"
clientConnection = self.socket.nextPendingConnection()
clientConnection.disconnected.connect(clientConnection.deleteLater)
print "server: socket state:",
print clientConnection.state()
if not clientConnection.waitForReadyRead(CLIENT_NO_REPLY_WAIT):
if self.DEBUG:
print "WARNING [server] Timeout waiting for client to transmit
data"
clientConnection.disconnectFromHost()
return
etc.
And the output looks like:
client: writing
client: sent
server: connected
server: socket state: 3
WARNING [server] Timeout waiting for client to transmit data
...so what's happening is the client is waiting for the connection to
establish and then writing immediately, but the server hasn't even begun
processing that the connection has been made yet. Then the server is
timing out waiting for the packet of information which has already flown
past.
Yet again this all works perfectly fine when run as two separate
programs.
Cheers
Steve
On Wed, 2010-07-07 at 20:21 +0200, mille kink wrote:
> Perhaps you could try to insert the 'local server' into a sperate
> thread.
>
> Kind regard,
> Ronny
>
>
> > On 7 Jul 2010 15:09, "Steve Castellotti" <sc at puzzlebox.info> wrote:
> >
> > Hello all-
> >
> > I have created two application classes using PyQt, one a server
> > which normally runs in console mode, the other a client with
> > complete GUI. The server opens a socket and listens for connections
> > from the client which issue various commands to be carried out.
> > These are bother working quite well.
> >
> > I would like to produce a third script which launches bother the
> > server and client processes at the same time, with all communication
> > happening over localhost. The point is to allow the user to run the
> > "network" version of the interface (connecting to the separate
> > server process over the network) or a "local" version which is
> > self-contained (including the server), while reusing the same code
> > as much as possible.
> >
> > This last piece is nearly working, however the server component
> > in the third "local" script is timing out during the communication
> > process. I am wondering if I am simply failing to properly
> > initialize the two class instances in order to share the single
> > event loop.
> >
> >
> > When launching the client application in "network" mode, the code
> > looks something like this:
> >
> > app = QtGui.QApplication(sys.argv)
> > window = client_interface(log)
> > window.show()
> > sys.exit(app.exec_())
> >
> >
> > When launching the server application from the console for "network"
> > mode, the code looks like this:
> >
> > # Perform KeyboardInterrupt handling
> > signal.signal(signal.SIGINT, signal.SIG_DFL)
> > app = QtCore.QCoreApplication(sys.argv)
> > server = network_server(log, server_interface, server_port)
> > sys.exit(app.exec_())
> >
> >
> > And when trying to launch both in the same event loop for the
> > "local" third script, I'm doing:
> >
> > app = QtGui.QApplication(sys.argv)
> > server = server.network_server(log, server_interface, server_port)
> > window = client.client_interface(log)
> > window.show()
> > sys.exit(app.exec_())
> >
> >
> >
> > In the server code, process control happens something like this:
> >
> > ...
> > self.socket.newConnection.connect(self.processConnection)
> > ...
> > def processConnection(self):
> > clientConnection = self.socket.nextPendingConnection()
> > if not clientConnection.waitForReadyRead(5000):
> > print "Timeout waiting for client to transmit data"
> > ...
> > etc.
> >
> >
> > Currently each time the client goes to send data to the server,
> > the connection seems to establish and the client side reports
> > writing to the socket, but the server side times out after five
> > seconds without the data packet having been received. The client GUI
> > interacts properly, and the server routines seem to be getting
> > called properly.
> >
> > The odd thing is I can start up the "local" integrated script,
> > which will launch the server process (opening and listening to the
> > port) as well as the client GUI, then connecting that server process
> > by launching an instances of the "network" mode client GUI (so now
> > there's two GUIs on the screen), and everything works fine with the
> > second GUI. So it would seem the server side of the "local" script
> > is working just fine.
> >
> >
> > Can anyone offer me pointers regards what I might be doing
> > wrong, or perhaps where to look next?
> >
> >
> > Cheers
> >
> > Steve Castellotti
> >
> >
> >
> >
> > _______________________________________________
> > PyQt mailing list PyQt at riverbankcomputing.com
> > http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100708/3b27c57e/attachment.html>
More information about the PyQt
mailing list