mulithreaded server

Jean-Paul Calderone exarkun at divmod.com
Tue Mar 11 12:10:14 EDT 2008


On Tue, 11 Mar 2008 08:24:54 -0700 (PDT), asit <lipun4u at gmail.com> wrote:
>import socket
>import sys
>import thread
>
>p=1
>PORT=11000
>BUFSIZE=1024
>
>def getData(cSocket):
>    global stdoutlock,cSocketlock
>    while True:
>        cSocketlock.acquire()
>        data=cSocket.recv(BUFSIZE)
>        if data=='q':
>            data='client exited'
>            cSocket.close()
>            p=0
>            cSocketlock.release()
>        stdoutlock.acquire()
>        stdout.write(data)
>        stdoutlock.release()
>
>def sendData(cSocket):
>    global stdoutlock,cSocketlock
>    while True:
>        stdoutlock.acquire()
>        data=raw_input('>>')
>        cSocketlock.acquire_lock()
>        if data=='q':
>            stdout.write('server exited')
>            stdout.release()
>            p=0
>            cSocket.close()
>        sSocket.send(data)
>        sSocketlock.release()

Could it be because `sSocketlock´ here

>
>
>stdout=sys.stdout
>host=''
>sSocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
>sSocket.bind((host,PORT,))
>sSocket.listen(1)
>#sSocketlock=thread.allocate_lock()

is never bound since the line above here is commented out?

>stdoutlock=thread.allocate_lock()
>print 'waiting for connection'
>cSocket,addr=sSocket.accept()
>print 'connection from',addr
>cSocketlock=thread.allocate_lock()
>thread.start_new_thread(sendData,(cSocket,))
>thread.start_new_thread(getData,(cSocket,))
>if p==0:
>    sSocket.close()
>
>
>
>In the above program, why there is an unhandeled exception ???

Just a guess. You should really include the traceback when you ask a
question like this.

Jean-Paul



More information about the Python-list mailing list