From: mk on
I have the following situation:

1.

self.conobj = paramiko.SSHClient()

self.conobj.connect(self.ip, username=self.username,
key_filename=self.sshprivkey, port=self.port, timeout=opts.timeout)

2. very slow SSH host that is hanging for 30+ seconds on key exchange.

The timeout in the options regards only a socket timeout, not further
stages of connection negotiation, so it doesn't work there.

On paramiko mailing list I got the suggestion to build a timer and then
quit this by myself:

> The timeout option in connect() is for the socket, not for the entire
> operation. You are connected, so that timeout is no longer relevant.
> You would probably have to wrap the transport.connect() method in a
> timer to break out of this early.

Question: how can I do that? Use another threaded class? Is there some
other way?


Additional snag is SSHClient() is a class that internally uses threads.
How do I kill brutally its threads? Is there any way to do it?

Regards,
mk

From: Steven D'Aprano on
On Fri, 05 Feb 2010 18:23:09 +0100, mk wrote:

[...]
> On paramiko mailing list I got the suggestion to build a timer and then
> quit this by myself:
>
>> The timeout option in connect() is for the socket, not for the entire
>> operation. You are connected, so that timeout is no longer relevant.
>> You would probably have to wrap the transport.connect() method in a
>> timer to break out of this early.
>
> Question: how can I do that? Use another threaded class? Is there some
> other way?

I don't use threads enough (or at all) to answer that question directly,
but I can point you at these. Hopefully they will help, or at least give
you some ideas:

http://code.activestate.com/recipes/534115/
http://code.activestate.com/recipes/473878/

Found by googling for "python timeout any function".



> Additional snag is SSHClient() is a class that internally uses threads.
> How do I kill brutally its threads? Is there any way to do it?

You can't kill threads in Python. You have to ask them nicely to die.

Google on "python kill thread" for more.




--
Steven
From: mk on
Stephen Hansen wrote:
> Question: how can I do that? Use another threaded class? Is there
> some other way?

First of all, thanks for answer!

> What OS? Does this have to be OS-independant?

Err, sorry -- this is Linux/UNIX only.

> Are you using more then
> one transport/SSLClient in your process and you want to just kill one
> (and any of its child threads), or are you doing one per process?

I'm using multiple threads myself, one per IP basically, which in turn
call paramiko calls, which itself is threaded.

> If you want to terminate -all- threads your process is running in a
> given timeframe, using a SIGALRM in the signal module will do it, I
> believe-- provided you don't need to support windows.

Thanks, that's still useful! Although...

1. if I don't install signal handler for SIGALRM I get this printed on
the console at the end of execution:

Alarm clock

Although it does seem to close cleanly.

Is there any way to suppress this message?


2. If I do install signal handler for SIGALRM, I'm back at square one:

Exception in thread Thread-25 (most likely raised during interpreter
shutdown):
Traceback (most recent call last):
File "/usr/lib/python2.4/threading.py", line 442, in __bootstrap
File "./cssh.py", line 1003, in run
File "./cssh.py", line 739, in ssh_connect_for_scp
File "build/bdist.linux-i686/egg/paramiko/transport.py", line 1006,
in connect
File "build/bdist.linux-i686/egg/paramiko/transport.py", line 1382,
in _log
exceptions.TypeError: 'NoneType' object is not callable
Unhandled exception in thread started by

This happens even though I surround "connect" (line 1006) with catching
all Exceptions:

try:
self.trans.connect(hostkey=None, username=self.username, pkey = pkey)
except Exception, e:
self.conerror = str(e)



>I had a contextlib
> manager do that for awhile. If you only want to terminate one (and its
> child-threads)... you're out of luck, I think. The only way to terminate
> a thread in Python is with conditions/events/whatever and the thread
> cooperating and exiting on its own.

I will probably have to get the library author look at this.

Regards,
mk

From: Aahz on
In article <mailman.1986.1265390557.28905.python-list(a)python.org>,
mk <mrkafk(a)gmail.com> wrote:
>
>self.conobj = paramiko.SSHClient()
>
>self.conobj.connect(self.ip, username=self.username,
>key_filename=self.sshprivkey, port=self.port, timeout=opts.timeout)
>
>2. very slow SSH host that is hanging for 30+ seconds on key exchange.
>
>The timeout in the options regards only a socket timeout, not further
>stages of connection negotiation, so it doesn't work there.

Does paramiko offer any kind of callback? In a similar situation with
pycurl, I built my own timer into a callback.
--
Aahz (aahz(a)pythoncraft.com) <*> http://www.pythoncraft.com/

"At Resolver we've found it useful to short-circuit any doubt and just
refer to comments in code as 'lies'. :-)"