I'm writing a little query function for a small file search engine and I have a little snag. After I've sent out the queries, I start a SocketServer.TCPServer listening for responses. The responses are coming back via XML-RPC. My problem is that I want to only accept responses for a given period of time after the query is sent out. If I use serve_forever, my client hangs.....if I use handle_request() I can only get 1 repsonse.
My initial solution was to put handle_request inside a while loop which will just check current time against start time and see if its been listening for x seconds or not. However, since handle_request appears to block, if noone responds, or if noone responds near the limited time, then it will just stay hanging waiting for a connection.
Does anyone know of a way around this?? I've started experimenting with socket timeouts but haven't gotten anywhere yet.....if this is going to be the only way to do it, does anyone know of a good online tutorial for this?
Did you know that there is an XML-RPC server module already in the standard library? However reading the docs, it doesn't seem to set a timeout either.
Both SimpleServer and SimpleXmlRpcServer expose the socket object, so I presume you could call socket.settimout(seconds) to set the timeout. I have not tried this, so I don't know how the server will handle the error that will be generated. The settimeout function is new to 2.3, so will not be portable to older version of Python.
Dave - The Developers' Coach