By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,478 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

100% CPU Usage when a tcp client is disconnected

P: n/a
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.
Can one tell what am I doing wrong?

#code.py

import SocketServer

class MyServer(SocketServer.BaseRequestHandler ):
def setup(self):
print self.client_address, 'connected!'
self.request.send('hi ' + str(self.client_address) + '\n')

def handle(self):
while 1:
data = self.request.recv(1024)
self.request.send(data)
if data.strip() == 'bye':
return

def finish(self):
print self.client_address, 'disconnected!'
self.request.send('bye ' + str(self.client_address) + '\n')

#server host is a tuple ('host', port)
server = SocketServer.ThreadingTCPServer(('', 50008), MyServer)
server.serve_forever()
Nov 22 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Nov 22, 9:53 am, Tzury Bar Yochay <Afro.Syst...@gmail.comwrote:
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
I forget exactly how the superclass works, but
that while 1 looks suspicious. Try chaning it
to

data = "dummy"
while data:
...

-- Aaron Watters

===
http://www.xfeedme.com/nucular/pydis...+infinite+loop
Nov 22 '07 #2

P: n/a
data = "dummy"
while data:
...
Thanks Alot
Nov 22 '07 #3

P: n/a
Tzury Bar Yochay <Af**********@gmail.comwrites:
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. Can one tell what am I doing wrong?
If you shut down telnet, self.request.recv(1024) returns an empty
string, meaning EOF, and you start inflooping.
def handle(self):
while 1:
data = self.request.recv(1024)
self.request.send(data)
if data.strip() == 'bye': # add: or data == ''
return
Nov 22 '07 #4

P: n/a
Thank Hrvoje as well
Nov 22 '07 #5

P: n/a
Aaron Watters wrote:
On Nov 22, 9:53 am, Tzury Bar Yochay <Afro.Syst...@gmail.comwrote:
>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

Nov 22 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.