It’s sounds like so stupid question since every developer who use any SSH library should have probably asked himself this question (?). But I can’t really find what is the difference between blocking or non-blocking…
I mean ok… One blocks till it receives the answer, the other sends the queries and returns immediately, then you check by yourself the reply buffer… I got that part.
But why to use one rather than the other? I can’t manage to find the answer…
Is it about performances? And if there is a difference, why?
Thanks in advance for any answer to this questions.
— Edit: Forget about the following “bonus question”, I’ve finally coded non blocking mode and experience the same problem, it must be something in libssh2. So I still don’t get the added value of non-blocking mode… —
Bonus question:
I’m not really sure could this difference explain something I’m experiencing?
I have a python script which connects to many hosts to run several commands.
It was using paramiko library in non-blocking mode. Paramiko is pure python and really slow for establishing ssh connections to many hosts…
I’m changing it for pylibssh2 which is python bindings for the C library libssh2. Since I didn’t get the difference, I started to code in blocking mode.Results:
– libss2 is much faster than paramiko (connection to 230 hosts in parallel in 4s instead of 1m30s)
– For running commands successively, libssh2 is also faster.
– When I run commands through ssh from several parallel threads, the code with libssh2 in blocking mode becomes slowlier than paramiko in non-blocking mode.
– I also noticed that the CPU consumption is very low compared with previous version. I guess part of this is related to C vs python but it seems than beyond the SSH API, my script itself performs less actions. Are threads blocking each other when sending commands through SSH in blocking mode?
The reason is that if you want to do two things at once, say read from some other network connection, and your SSH session, you have two options:
use blocking APIs, and use two threads or processes so you can do them both
use non-blocking APIs so the same thread can do both
This latter approach is called Asynchronous I/O. See for example twisted which uses it extensively.