Asynchronous SSH tasks

I have managed to get copies working well between Windows and Linux
but I noticed that the libraries allow Asynchronous operation. I can
send Async commands and the loop till they complete but copies seem to
stay busy forever. Maybe I am just not cleaning up right (it does not
seem so)

ssh_session = Net::SSH.start(…etc…)

ssh_session.exec(“sleep 10”)

ssh_session.sftp.connect.upload(src_file, tgt_file)

ssh_session.loop(1) {
puts “I=#{interrupted} B=”+@ssh_session.busy?.inspect
not interrupted and @ssh_session.busy?
}

If the sleep is called (instead of the upload) it waits 10 secs and
then is not busy and wakes.
But the copy will not wake. Since I have hundreds of files to copy
I’d like to set them all off, do some other work, and then wait for
them to complete (rather than wait for them one at a time with
upload!) and then do the other work.

Any help would be appreciated… should I cleanup the “sftp.connect”?
How? When (if it is asynchronous)?

Thanks

Found the solution

ssh_session = Net::SSH.start(…etc…)

sftp_conn = ssh_session.sftp.connect! # needed to block connect here
(!) or the upload seems to hang
sftp_conn.upload(src_file, tgt_file)

interrupted = false
trap(‘INT’) { interrupted = true }
sftp_conn.loop() {
not interrupted and sftp_conn.pending_requests.any? # check for
pending requests
}
sftp_conn.close_channel()
ssh_session.loop(1) {
not interrupted and ssh_session.busy?
}
sftp_conn = ssh_session.sftp.connect!

One word of warning if sending lots of files (>300) fast then you may
run out of open file handles (Windows).
I avoid the error by doing this call each now and then when starting
another upload

if (sftp_conn.pending_requests.length>=100)
interrupted = false
trap(‘INT’) { interrupted = true }
sftp_conn.loop() {
not interrupted and sftp_conn.pending_requests.any?
}
end

I guess I could have exited when the queue was smaller not empty also.

Hope this helps someone else.