socket.rcv timeout while-loop
Hi everybody, I am using Python 3.0. I have such a code : b=time.clock() while time.clock()-b<3 : data=s.recv(1024) However I would like to set timeout on the socket rcv method, so that the while loop stops exactly after 3 seconds. Is this possible ? Thanks a lot, Dwayne -- http://mail.python.org/mailman/listinfo/python-list
Re: socket.rcv timeout while-loop
Thanks for your answer. I don't want to reset my socket. I want to apply the timeout to the rcv method only. What about select ? http://docs.python.org/library/select.html#select.select How to implement it ? Thanks a lot, Dwayne 2011/2/3 Stephen Hansen > On 2/3/11 9:56 AM, Dwayne Blind wrote: > > However I would like to set timeout on the socket rcv method, so that > > the while loop stops exactly after 3 seconds. Is this possible ? > > I rarely do low-level socket stuff -- but I think s.settimeout() is what > you're looking for. It applies to the whole socket, and not just one > method -- so you may want to reset it after you're done recv'n. > > -- > > Stephen Hansen > ... Also: Ixokai > ... Mail: me+list/python (AT) ixokai (DOT) io > ... Blog: http://meh.ixokai.io/ > > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- http://mail.python.org/mailman/listinfo/python-list
Re: socket.rcv timeout while-loop
Thanks Stephen. It's really nice of you. I have not understood everything though. (I have never used a context manager before.) Here are some comments : timeout = s.gettimeout()# Is that the default timeout ? s.settimeout(3) # I guess this is a 3 second timeout s.recv(1024) s.settimeout(timeout) # You change it back ? So with a while loop, it should be : timeout = s.gettimeout() s.settimeout(3) b=time.clock() while time.clock()-b<3 : data=s.recv(1024) s.settimeout(timeout) Am I right ? Thanks again, Dwayne 2011/2/3 Stephen Hansen > On 2/3/11 10:13 AM, Dwayne Blind wrote: > > Thanks for your answer. I don't want to reset my socket. I want to apply > > the timeout to the rcv method only. > > Setting the timeout does not "reset [your] socket", I don't think. And I > get that you want to only timeout recv... that's why I pointed out its a > socket method, not an argument to recv. If you don't want it to apply to > everything else, you just have to be sure to change it back after recv. > > Just: > timeout = s.gettimeout() > s.settimeout(3) > s.recv(1024) > s.settimeout(timeout) > > Personally, I'd prefer to do: > > with timeout(s, 3): >s.recv(1024) > > That's a lot more clear, and I'd roll this context manager to accomplish > it: > > --- start > > from contextlib import contextmanager > > @contextmanager > def timeout(sock, timeout): >old_timeout = sock.gettimeout() >sock.settimeout(timeout) >try: >yield sock >finally: >sock.settimeout(old_timeout) > > --- end > > The contextmanager decorator is an easy/quick way of making a context > manager. Everything up until the yield is executed before the 'with' > block is run, and everything after the yield is executed after the > 'with' block concludes. > > If the with block throws an exception, it'll be catchable at the yield > point. > > -- > > Stephen Hansen > ... Also: Ixokai > ... Mail: me+list/python (AT) ixokai (DOT) io > ... Blog: http://meh.ixokai.io/ > > -- http://mail.python.org/mailman/listinfo/python-list
Re: socket.rcv timeout while-loop
The solution would be timeout = s.gettimeout() s.settimeout(3) b=time.clock() while time.clock()-b<3 : try : data=s.recv(1024) except : break s.settimeout(timeout) Am I right ? Dwayne 2011/2/4 Dwayne Blind > Thanks Stephen. It's really nice of you. > > I have not understood everything though. (I have never used a context > manager before.) > > Here are some comments : > > timeout = s.gettimeout()# Is that the default timeout ? > s.settimeout(3) # I guess this is a 3 second timeout > s.recv(1024) > s.settimeout(timeout) # You change it back ? > > So with a while loop, it should be : > > > timeout = s.gettimeout() > s.settimeout(3) > b=time.clock() > while time.clock()-b<3 : > > data=s.recv(1024) > s.settimeout(timeout) > > Am I right ? > > Thanks again, > Dwayne > > > 2011/2/3 Stephen Hansen > >> On 2/3/11 10:13 AM, Dwayne Blind wrote: >> >> > Thanks for your answer. I don't want to reset my socket. I want to apply >> > the timeout to the rcv method only. >> >> Setting the timeout does not "reset [your] socket", I don't think. And I >> get that you want to only timeout recv... that's why I pointed out its a >> socket method, not an argument to recv. If you don't want it to apply to >> everything else, you just have to be sure to change it back after recv. >> >> Just: >> timeout = s.gettimeout() >> s.settimeout(3) >> s.recv(1024) >> s.settimeout(timeout) >> >> Personally, I'd prefer to do: >> >> with timeout(s, 3): >>s.recv(1024) >> >> That's a lot more clear, and I'd roll this context manager to accomplish >> it: >> >> --- start >> >> from contextlib import contextmanager >> >> @contextmanager >> def timeout(sock, timeout): >>old_timeout = sock.gettimeout() >>sock.settimeout(timeout) >>try: >>yield sock >>finally: >>sock.settimeout(old_timeout) >> >> --- end >> >> The contextmanager decorator is an easy/quick way of making a context >> manager. Everything up until the yield is executed before the 'with' >> block is run, and everything after the yield is executed after the >> 'with' block concludes. >> >> If the with block throws an exception, it'll be catchable at the yield >> point. >> >> -- >> >> Stephen Hansen >> ... Also: Ixokai >> ... Mail: me+list/python (AT) ixokai (DOT) io >> ... Blog: http://meh.ixokai.io/ >> >> > -- http://mail.python.org/mailman/listinfo/python-list
Re: socket.rcv timeout while-loop
or rather timeout = s.gettimeout() b=time.clock() while time.clock()-b<3 : s.settimeout(3-time.clock()+b) try : data=s.recv(1024) except : break s.settimeout(timeout) Sorry for all these messages Dwayne 2011/2/4 Dwayne Blind > The solution would be > > > timeout = s.gettimeout() > s.settimeout(3) > b=time.clock() > while time.clock()-b<3 : >try : >data=s.recv(1024) > except : >break > s.settimeout(timeout) > > Am I right ? > > Dwayne > > 2011/2/4 Dwayne Blind > > Thanks Stephen. It's really nice of you. >> >> I have not understood everything though. (I have never used a context >> manager before.) >> >> Here are some comments : >> >> timeout = s.gettimeout()# Is that the default timeout ? >> s.settimeout(3) # I guess this is a 3 second timeout >> s.recv(1024) >> s.settimeout(timeout) # You change it back ? >> >> So with a while loop, it should be : >> >> >> timeout = s.gettimeout() >> s.settimeout(3) >> b=time.clock() >> while time.clock()-b<3 : >> >> data=s.recv(1024) >> s.settimeout(timeout) >> >> Am I right ? >> >> Thanks again, >> Dwayne >> >> >> 2011/2/3 Stephen Hansen >> >>> On 2/3/11 10:13 AM, Dwayne Blind wrote: >>> >>> > Thanks for your answer. I don't want to reset my socket. I want to >>> apply >>> > the timeout to the rcv method only. >>> >>> Setting the timeout does not "reset [your] socket", I don't think. And I >>> get that you want to only timeout recv... that's why I pointed out its a >>> socket method, not an argument to recv. If you don't want it to apply to >>> everything else, you just have to be sure to change it back after recv. >>> >>> Just: >>> timeout = s.gettimeout() >>> s.settimeout(3) >>> s.recv(1024) >>> s.settimeout(timeout) >>> >>> Personally, I'd prefer to do: >>> >>> with timeout(s, 3): >>>s.recv(1024) >>> >>> That's a lot more clear, and I'd roll this context manager to accomplish >>> it: >>> >>> --- start >>> >>> from contextlib import contextmanager >>> >>> @contextmanager >>> def timeout(sock, timeout): >>>old_timeout = sock.gettimeout() >>>sock.settimeout(timeout) >>>try: >>>yield sock >>>finally: >>>sock.settimeout(old_timeout) >>> >>> --- end >>> >>> The contextmanager decorator is an easy/quick way of making a context >>> manager. Everything up until the yield is executed before the 'with' >>> block is run, and everything after the yield is executed after the >>> 'with' block concludes. >>> >>> If the with block throws an exception, it'll be catchable at the yield >>> point. >>> >>> -- >>> >>> Stephen Hansen >>> ... Also: Ixokai >>> ... Mail: me+list/python (AT) ixokai (DOT) io >>> ... Blog: http://meh.ixokai.io/ >>> >>> >> > -- http://mail.python.org/mailman/listinfo/python-list
Re: socket.rcv timeout while-loop
Thanks to all of you. @ Jean-Michel Pichavant I am writing a small multiplayer game. Several clients are connected to the server. Games last, say, 20 seconds. You can think of the game as a small chat lasting 20 seconds. All the data received by the server is sent back to the clients. @ Stephen Hansen Each player can send as many words as he wants. I think this is why I need the loop. Don't you think so ? @ Dave Angel s = conn, with conn,address=socket.accept() (see Socket Objects) Thanks again 2011/2/4 Jean-Michel Pichavant > Stephen Hansen wrote: > >> On 2/3/11 9:56 AM, Dwayne Blind wrote: >> >> >>> However I would like to set timeout on the socket rcv method, so that >>> the while loop stops exactly after 3 seconds. Is this possible ? >>> >>> >> >> I rarely do low-level socket stuff -- [snip] >> >> >> > Good point. Python has a module for almost anything you would need to do on > a network. Make sure none of these modules fit your needs. > You could tell us what you want to achieve at a higher level, we may point > you to a already existing module. > > Some of these modules are life saver, as I personnally always wrote buggy > netcode for whatever reason :( > > JM > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: socket.rcv timeout while-loop
Thank you very much Jean-Michel Pichavant and Stephen Hansen. @ Jean-Michel Pichavant I will have a look at Pyro. @ Stephen Hansen Now I am pretty much worried. :'( 2011/2/4 Stephen Hansen > On 2/4/11 6:55 AM, Dwayne Blind wrote: > > @ Jean-Michel Pichavant > > I am writing a small multiplayer game. Several clients are connected to > > the server. Games last, say, 20 seconds. > > You can think of the game as a small chat lasting 20 seconds. All the > > data received by the server is sent back to the clients. > > > > @ Stephen Hansen > > Each player can send as many words as he wants. I think this is why I > > need the loop. Don't you think so ? > > No. I've never seen or used network code which operated in any way like > that. Sometimes you want code which blocks, sometimes you want code that > doesn't: sometimes you want stuff that may block for a little while, but > with a timeout. > > I can't even imagine why you'd want code which aggressively tries to > read for multiple seconds before moving on. > > Either the read works and you have data; or it doesn't, and you move on > to do something else and try again later. Perhaps after trying to read > from another socket-- or, perhaps after a select.select() call tells you > there's something more to read. T > > But you need to separate the logic of your game from this network > infrastructure. > > From your game logic perspective, perhaps you process the responses line > by line. From your network logic perspective, you may happen to get one > character at a time-- or it may burst to you all at once. The socket > interfaces will try to give you up to the requested number of bytes but > the network layer has every possibility of just giving you partial > responses. > > So the network layer should just gather up the data as it arrives, > buffer it -- and pass it off to the game logic layer as each line is > complete (i.e., as \r\n or \n's are received). But there's no reason at > all to do a while loop to aggressively try to read from one particular > socket repeatedly for multiple seconds. At least, none that I've ever > run into. > > Granted, I'm not at all a master of socket-fu. > -- > > Stephen Hansen > ... Also: Ixokai > ... Mail: me+list/python (AT) ixokai (DOT) io > ... Blog: http://meh.ixokai.io/ > > -- http://mail.python.org/mailman/listinfo/python-list
Re: socket.rcv timeout while-loop
Dear Stephen, Thanks for telling me this all looked very peculiar. As you said, it did not really need solving. Cheers, Dwayne 2011/2/4 Stephen Hansen > On 2/4/11 9:16 AM, Dwayne Blind wrote: > > @ Stephen Hansen > > Now I am pretty much worried. :'( > > Why? This is all sounding like a problem that isn't actually a problem. > > I think you may have over-analyzed yourself into a corner and think you > have something to solve which doesn't really need solving. :) > > -- > > Stephen Hansen > ... Also: Ixokai > ... Mail: me+list/python (AT) ixokai (DOT) io > ... Blog: http://meh.ixokai.io/ > > -- http://mail.python.org/mailman/listinfo/python-list