开发者

Python's paramiko with threading, strange delay

开发者 https://www.devze.com 2023-03-23 03:17 出处:网络
In order to check the existence of a certain file on many servers, I\'ve created a threaded class that uses paramiko to execute a remote command using ssh. The results of each thread are inserted into

In order to check the existence of a certain file on many servers, I've created a threaded class that uses paramiko to execute a remote command using ssh. The results of each thread are inserted into a thread safe Queue.Queue, and I read its contents after all the join()s are done. This is the main part of the thread:

client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect(self.server, username='linqmap', timeout=5)
stdin, stdout, stderr = client.ex开发者_StackOverflowec_command('my_command')

This solution works great, but there is a strange behavior that got my attention. The thread logs its __init__ time:

def __init__(self, server, queue, lock):
    ...
    self.start_time=datetime.datetime.now()
    ...

And its finish time (the time where the results was inserted into the queue):

def _report(self, message, status):
    duration=datetime.datetime.now()-self.start_time
    ...
    self.queue.put( (self.server, duration, message, status) )

The problem is that the duration is very similar for all the threads. For a small number (~5) of threads, it is about 3 seconds. For larger numbers (~100), it is about 30 seconds.

Since there is a timeout of 5 seconds in the connection, I presumed all the threads will either finish in 5 seconds or fail. Any idea what happened here?


The timeout is just for the TCP connect, not the entire operations on the connection.

I would expect the __init__ to be called on the original thread - only the run() method runs on the new thread. So your time might be invalid.

SSH involved a certain amount of processing for the encryption and setup of the tunnel, so it may be worth looking at the CPU usage on the client, to see if that is your limiting factor for 100's of connections.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号