Post by Nikolaus RathHello,
I would like to use an SFTPClient instance concurrently with several
threads, but I couldn't find any information about thread safety in the
API documentation.
- Can I just share the SFTPClient instance between several threads?
Why use SFTPClient? Its performance might not be very good plus
compatibility issues with some SSH servers might crop up. I know I had
compatibility issues even with some pretty standard SSH servers on some
platforms (esp. Solaris).
I would avoid SFTPClient if I were you.
I wrote my own multithreaded SCP class (handling both upload and
download) which I can post if you're interested. It's been in use for a
while by several users and I think it's pretty well debugged by now.
All I need is a Python API for uploading, downloading and renaming files
over SSH. I chose SFTPClient since it seemed to be the simplest
solution, and I don't remember seeing any warnings about performance or
compatibility. Can you tell me what exactly the problem with SFTPClient
is? Are there any better options within paramiko? In any case, I am
certainly interested in taking a look at your solution.
for instance, what if you need to close down the thread in the middle of
operation but SFTPClient doesn't allow that?
When I'm in the middle of an operation, then I am in the middle of an
SFTPClient method. Obviously I can't shut down the thread while the
interpreter is not executing my code. This doesn't seem to be an
SFTPClient or even multithreading specific problem to me.
What if you're shutting
down an interpreter and SFTPClient throws an exception which is visible
for end user?
The interpreter should keep running while at least one thread is alive.
It seems to me that if SFTPClient throws an exception, obviously
something went wrong and it is a good think to know about it.
What if there's no SCP/sftp on the other end (and this
does happen from time to time)
If the user tries to establish an SFTP connection to a server that does
not support SFTP, then things will obviously break. But that's not a bug
in the program.
I have done quite a lot of work on
getting my class to work reasonably under such circumstances: for
instance, thread's file sending/downloading methods watch value of
thread.abort flag and if it's set to True by an external class, they
shut down gracefully.
I think you are using multithreading in a different way. Let me guess:
you have a main thread that has to stay responsive and therefore
delegates time-consuming operations to individual worker threads. These
worker threads must shut down when the main threads asks them to do so.
In this situation you have to deal with the problems you describe, but
they are not specific to SFTPClient and they do not always arise when
using multiple threads.
For example, my application is much simpler. I have several threads
which work independently of each other. There is no main controlling
thread. The application terminates when all the threads have finished
their work. (I am essentially programming a server with individual
threads handling client requests).
remembering to sleep just in case after releasing locks to prevent
starvation
I never heard of that. Could you explain in more detail what you mean?
Best,
-Nikolaus
--
»Time flies like an arrow, fruit flies like a Banana.«
PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C