[paramiko] Multi-Threading and paramiko

Robey Pointer robey at lag.net
Fri Sep 14 19:28:04 PDT 2007


On 10 Sep 2007, at 15:16, jd wrote:

> Hi everyone,
>
> I remember reading somewhere.. that paramiko is thread
> safe library and can be used in multi-threaded
> programs.
>
> Can some one please confirm it. I can not spot that in
> to doc.

That's correct. Paramiko is thread-safe and uses threads internally.


> Are there any restrictions / required affinity between
> paramiko objects (transport / channel ) and threads in
> which they are used ?

No, you should be able to call into paramiko from any thread. You  
still need to be careful about synchronizing your own data  
structures, though.

When you open a new Transport in paramiko, it creates a thread  
devoted to handling incoming data. The incoming data is parsed and,  
if it's data for an open channel, the data is dropped into a buffer  
for that channel. If any thread is waiting for data on that channel,  
it will immediately be woken up.

One way to envision this is that you can open a Transport and Channel  
within one thread, then immediately block on a read() call on that  
channel. When the data arrives, the paramiko thread for that  
Transport will drop it into the buffer for that channel, wake up the  
thread blocked in read(), and that thread will immediately pull data  
from the buffer and return.

robey




More information about the paramiko mailing list