Embedded Threading Question

Tim Dietz tpdietz at hotmail.com
Mon Dec 9 10:27:29 EST 2002


> > 
> > OK, I'm getting closer.  I've trimmed down my Python code so that it
> > has a thread to monitor the socket (ReadThread) as well as the main
> > thread.
> > 
> > So, to simplify, there is a function (called SendMsg) that sends text
> > to a TCP/IP server using socket.send().  The server sends a response
> > back to the client.  The response is picked up in ReadThread and
> > processed from there.  However, my problem occurs whether or not I
> > process the data read from the socket, so I've eliminated that code. 
> > In addition, the server can send asynchronous updates to this client,
> > which are also processed in the ReadThread.
> > 
> > So, below is the streamlined version of my Python module (client.py):
> > 
> > def SendMsg(outmsg):
> >    mysock.send(outmsg)
> > 
> > def ReadThread():
> >    done = 0
> >    buf = ''
> >    while not done:
> >       rcv,wr,err = select.select([mysock],[],[],0.1)
> >       if mysock in rcv:
> >          buf += rcv[0].recv(1000)
> 
> I've experimented a bit with a similar function:
> 
> done = 0
> queue = Queue.Queue()
> def ReadThread(mysock):
>     global queue, done
>     while 1:
>             rcv,wr,err = select.select([mysock],[], [])
>             if mysock in rcv:
>                 data = mysock.recv(1024)
>                 print 'Receiving: ', data 
>                 queue.put('ReadThread: ' + data)                
>                 if data.endswith('QUIT'):
>                     done = 1
>                     return
> 
> def UseReadThread1():
>     global done
>     done = 0
>     mysock = socket(AF_INET, SOCK_STREAM)      
>     mysock.connect((serverHost, serverPort))   
>     thread.start_new(ReadThread, (mysock,))
>     print "Sending ASYNCH"
>     mysock.send("ASYNCH")
>     time.sleep(2)        
>     while not done:
>         time.sleep(1)
>     mysock.close()        
> 
> As server I used a simple echo-server.
> 
> Function ReadThread ran fine, also when running in a separate thread.
> It also ran fine when I kept the socket open and let the server send
> aynchronous updates back to the client. ("ASYNCH" would cause it to do
> that; at some random moment it would echo back "QUIT" and close the
> connection.)
> When run in a GUI like IDLE, UseReadThread would generally cause
> problems, even when run in a separate thread. (For instance, first run
> would be ok, second one would hang IDLE).  But this was caused by the
> two print statements (in ReadThread and in UseReadThread); commenting
> them out, made it work fine in IDLE as well.
> 
> I don't quite understand why you'd want to separate sending and
> receiving, and why you use a function like ReadThread at all. It seems
> more natural (and less error-prone) to me to spawn a separate thread
> for each client request, create a client socket inside that thread,
> and there wait for the server to respond.
> 
> Hans

The example that I gave was the smallest bit of code that I thought
would get the point across.  I didn't want to add anything that could
distract from understanding the basics of what the problem was.

The reason I need a separate thread for my problem (correct me if you
have a better idea) is that each client not only needs to send a
message and then wait for a response to that message, it also needs to
be listening for updates that could come at any time from the server. 
I don't know any other way to do that other than making the client a
server as well, and that seems like overkill.

So, I'm probably not setting the C part of the Python interface up
properly, because if the client works fine in DOS Python, but not when
run from my app, we know where the fault lies.  However, I've
exhausted my resouces and haven't got a clue as to what to do next.

Anyway, thank you for your patience and trying to help, Hans.


Tim



More information about the Python-list mailing list