http://stackoverflow.com/questions/15311450/my-chat-client-freezes-up-after-beginning-threads

Andrew Berg bahamutzero8825 at gmail.com
Sat Mar 9 11:35:49 EST 2013


On 2013.03.09 09:26, Owatch wrote:
> Thing is, when I run the program. Nothing happens. 
> 
> Can somebody help point out what is wrong? (I've asked questions and researched for 3 days, without getting anywhere, so I did try)
You defined a thread, but never created or started it. Also, why did you
subclass threading.Thread? You also mentioned queues, but you didn't use
them.

Not tested, but shows the basics:

import threading
import queue
import socket

def process():
  while alive:
    thing = things_to_process.get()
    # do stuff with the thing here
    things_to_process.task_done()

alive = True
host = 'localhost'
port = 9999
things_to_process = queue.Queue()
process_thread = threading.Thread(target=process)
process_thread.start()
sock = socket.socket()
sock.connect((host, port))
while alive:
  try:
    data = sock.recv()
  except Exception: # should probably do different things for different
errors in real code
    alive = False
    sock.close()
    process_thread.join()
    raise
  else:
    things_to_process.put(data)

-- 
CPython 3.3.0 | Windows NT 6.2.9200 / FreeBSD 9.1



More information about the Python-list mailing list