Thread error

Salvatore Di Fazio salvatore.difazio at gmail.com
Sun Dec 3 15:31:01 EST 2006


Hi guys,
when I close the application I get the following error:

-----------------------------
Traceback (most recent call last):
  File "main.py", line 88, in <module>
    while exit : pass
KeyboardInterrupt
Unhandled exception in thread started by
Error in sys.excepthook:

Original exception was:
-----------------------------

This is the code:
-----------------------------
# Echo client program
import socket
import sys
import thread
import mtalk

HOST = '192.168.0.115'    # The remote host
PORT = 3580               # The same port as used by the server

buff = ""
buffStatus = 0
s = None
exit = 1

############### keywordDataIn ###############
def keywordDataIn():
  global buff, buffStatus, exit
  talkmsg = mtalk.TalkMessage()
  while exit:
    line = sys.stdin.readline()
    if line == 'quit\n':
      exit = 0
      break
    elif line != '\n':
      lock.acquire()
      buff = talkmsg.Make(line)
      buffStatus = 1
      lock.release()
    elif line == '\n':
      pass

############### dataToServer ###############
def dataToServer():
  global buff, buffStatus, exit
  while exit:
    if buffStatus == 1:
      try:
        lock.acquire()
        s.sendall(buff)
        buff = ""
        buffStatus = 0
        lock.release()
      except: socket.error
      pass # errore da controllare

############## dataFromServer ##############
def dataFromServer():
  global exit
  while exit:
    data = s.recv(1024)
    print 'Received', repr(data)




############### Main ###############
if __name__ == "__main__" :

  for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC,
socket.SOCK_STREAM):
    af, socktype, proto, canonname, sa = res

  buffStatus = 0

  try:
    s = socket.socket(af, socktype, proto)
  except socket.error, msg:
    print("Unexpected error")
    s = None
    sys.exit(1)

  try:
    s.connect(sa)
    print 'Connection ...'
  except socket.error, msg:
    print("The server was not reachable")
    s.close()
    s = None
    sys.exit(1)

  print 'Connected'

  # mutex
  lock = thread.allocate_lock()
  thread.start_new_thread(keywordDataIn, ())
  thread.start_new_thread(dataToServer, ())
  thread.start_new_thread(dataFromServer, ())

  while exit : pass

  s.close()
-----------------------------


Tnx




More information about the Python-list mailing list