Kill thread or at least socket.getaddrinfo

  • Thread starter Thomas Dybdahl Ahle
  • Start date
T

Thomas Dybdahl Ahle

Hi, I'm writing an application that connects to the internet.
Something like this:

for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
self.sock = socket.socket(af, socktype, proto)

Now if the user press the cancel button, I'd like the connection to
imidiatly stop. I run
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close()
Dunno if they are both nessesary. I normaly use only the first, but it
makes no difference to use both.

If python is at the actual connection in socket.socket( this work fine,
but if python is at calling socket.getaddrinfo(, it doesn't stop.

I also can't kill the thread, as it is afaik not a possibility in python.
Is there any other way to do this?
 
K

kyosohma

Hi, I'm writing an application that connects to the internet.
Something like this:

for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
self.sock = socket.socket(af, socktype, proto)

Now if the user press the cancel button, I'd like the connection to
imidiatly stop. I run
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close()
Dunno if they are both nessesary. I normaly use only the first, but it
makes no difference to use both.

If python is at the actual connection in socket.socket( this work fine,
but if python is at calling socket.getaddrinfo(, it doesn't stop.

I also can't kill the thread, as it is afaik not a possibility in python.
Is there any other way to do this?


This is addressed to some degree on a wxPython wiki at:

http://wiki.wxpython.org/index.cgi/LongRunningTasks

I think you can also use the join() method. I've also heard that if
you know the pid, you can kill it, but that's not always a clean way
of accomplishing the task.

Mike
 
L

lobais

you know the pid, you can kill it, but that's not always a
clean way of accomplishing the task.

So I have to open the connection in a new process... Sigh.. How I hate
this part of Python.
 
C

Chris Mellon

So I have to open the connection in a new process... Sigh.. How I hate
this part of Python.

This isn't a python problem. You can't cleanly or safely kill threads,
period. If you have to use blocking functions like this (you don't,
you might consider using Twisted for your networking instead) the way
you "cancel" it is to just stop waiting for the response and discard
the response (or error) when it eventually comes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top