100% CPU Usage when a tcp client is disconnected

Scott David Daniels Scott.Daniels at Acm.Org
Thu Nov 22 12:49:53 EST 2007


Aaron Watters wrote:
> On Nov 22, 9:53 am, Tzury Bar Yochay <Afro.Syst... at gmail.com> wrote:
>> The following is a code I am using for a simple tcp echo server.
>> When I run it and then connect to it (with Telnet for example) if I
>> shout down the telnet the CPU tops 100% of usage and saty there
>> forever....
>>     def handle(self):
>>         while 1:
>>             data = self.request.recv(1024)
>>             self.request.send(data)
>>             if data.strip() == 'bye':
>>                 return
> ... Try changing it to ...
> data = "dummy"
> while data:
>     ...

Even better:
     from functools import partial

     def handle(self):
         for data in iter(partial(self.request.recv, 1024), ''):
             self.request.send(data)
             if data.strip() == 'bye':
                 break
         else:
             raise ValueError('Gone w/o a "bye"')  # or IOError

-Scott




More information about the Python-list mailing list