On 07Jan2016 14:26, richard kappler <richkapp...@gmail.com> wrote:
See previous posts on 'looping generator' for details about the code and
project.
The brief version, I am reading and parsing a data stream through a socket,
several actually. Each new connection spawns a thread that reads and
parses. Should the client close, I want the thread to terminate. Everything
I've read says you don't kill threads, but if the client closes, the socket
closes, and the thread is just sitting there, hanging.
If the socket.recv returns 0 bytes, the docs tell me that means the client
closed and therefore the server socket closes as well. If I do something in
the thread target function like:
data_chunks = connection.recv(8192)
if len(data_chunks) == 0:
break
len(data_chunks)==0 tells me the socket is closed, but does the break kill
the thread? How do I prove that if it does?
The break doesn't kill the thread, but if the recv loop is all your thread's
main function does, then sure: when the function exits, the thread exits. For
example:
T = Thread(target=gather_func)
T.start()
...
def gather_func():
while True:
data_chunks = connection.recv(8192)
if len(data_chunks) == 0:
break
... do stuff with data_chunks ...
Then you break exits the loop. Then the function returns since there's no more
code in the function after the loop. And then the Thread exits because the
function it was running has finished. There is no need to kill the Thread here.
You can wait for the Thread with T.wait() and probe it with T.is_alive(); see
the threading module documentation.
BTW, I still recommend you drop use of .recv() and use .read1() to assemble
packets. See your main discussion.
Cheers,
Cameron Simpson <c...@zip.com.au>
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor