Sockets and threads

Byron Morgan lazypointer at yahoo.com
Thu Jun 12 12:16:39 EDT 2003


"Alan Kennedy" <alanmk at hotmail.com> wrote in message
news:3EE6FEE7.8CFF39B5 at hotmail.com...
> Byron Morgan wrote:
>
> > If I monitor a socket connection or even jsust listen to a socket in a
> > thread, Python's cpu usage expands to use all available processor time.
>
> Hmm, this sounds like something is wrong, for sure.
>
> A few questions that would be helpful if you could answer:
>
> 1. How many threads are you creating? At what stage of connection
processing?
> 2. How many of those threads are 'listen'ing for socket connections?
> 3. If you answered "more than 1" for question 2, that's a problem.
> 4. When you receive a new connection, do you create a new thread, or pass
> control to an existing thread?
> 5. Does each thread do blocking or non-blocking reads on the socket?
>
> If you can answer those, it should hopefully become plainly clear what is
going
> wrong.
>
> -- 
> alan kennedy
> -----------------------------------------------------
> check http headers here: http://xhaus.com/headers
> email alan:              http://xhaus.com/mailto/alan
Alan Kennedy asks,

> 1. How many threads are you creating? At what stage of connection
processing?
> 2. How many of those threads are 'listen'ing for socket connections?
> 3. If you answered "more than 1" for question 2, that's a problem.
> 4. When you receive a new connection, do you create a new thread, or pass
> control to an existing thread?
> 5. Does each thread do blocking or non-blocking reads on the socket?


Alan,

1. This happens with just a single thread (plus the main thread). But what I
need to do is run sockets with several threads, and I can do it just fine,
except that Python hogs the machine resources if I do.
2. I have had the same effect with just one thread listening for a
connection.
3. Why do you say more than 1 would be a problem?
4. Haven't gone further than just listening.
5. I have only used blocking style reads, but I do use timeout values.

Here are two code samples - sample 1 runs very politely, using minmal cpu.
Sample 2 causes the cpu usage meter to max out to 100% unless there is a
sleep
cycle in main(), in which case cpu usage looks very much like sample 1
(but I need for my apps to do stuff, not spend the day sleeping).

Byron Morgan
-------------------------------------
# tnet1.py
# monitor a telnet stream without threading
from telnetlib import Telnet
import msvcrt
host = 'localhost'
port = '12345'
session = Telnet()
session.open(host,port)

while not msvcrt.kbhit():
    txt = session.read_until('\n',.5)
    #seems to make no difference whether a timeout value is used or not.
    if txt.strip() is not '':
        print txt.strip()

session.close()
# --------- end of sample 1

#tnet2.py
# monitor a telnet stream in a thread
from telnetlib import Telnet
from time import *
import msvcrt
import threading

host = 'localhost'
port = '12345'
session = Telnet()
session.open(host,port)

# subclass of threading.thread taken from W.Chun's Core Python.
class MyThread(threading.Thread):
 def __init__(self, func, args, name = ''):
  threading.Thread.__init__(self)
  self.name = name
  self.func = func
  self.args = args

 def getResult(self):
  return self.res

 def run(self):
  print 'starting', self.name, 'at:', \
     ctime(time())
  self.res = apply(self.func, self.args)
  print self.name, 'finished at:', ctime(time())

def telnetreader():
    while 1:
        txt = session.read_until('\n',.5)
        if txt.strip() is not '':
            print txt.strip()

telgo = MyThread(telnetreader,(),telnetreader.__name__)
telgo.setDaemon('daemonic')
telgo.start()

def main():
    while not msvcrt.kbhit():
        pass
        #sleep(5)
        #uncomment sleep statement and cpu usage returns to normal.
    session.close()

if __name__ == '__main__':
    main()
#---------- end of sample 2 -----------






More information about the Python-list mailing list