471,078 Members | 822 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,078 software developers and data experts.

Kill thread or at least socket.getaddrinfo

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?
Mar 26 '07 #1
3 2752
On Mar 26, 10:53 am, Thomas Dybdahl Ahle <lob...@gmail.comwrote:
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

Mar 26 '07 #2
On 26 Mar., 18:08, kyoso...@gmail.com wrote:
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.

Apr 10 '07 #3
On 10 Apr 2007 11:07:51 -0700, lo****@gmail.com <lo****@gmail.comwrote:
On 26 Mar., 18:08, kyoso...@gmail.com wrote:
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.
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.
--
http://mail.python.org/mailman/listinfo/python-list
Apr 10 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by phil | last post: by
3 posts views Thread by Salvatore Di Fazio | last post: by
1 post views Thread by mirandacascade | last post: by
18 posts views Thread by =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post: by
20 posts views Thread by =?ISO-8859-1?Q?Gerhard_H=E4ring?= | last post: by
1 post views Thread by Karl Chen | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.