Hi there, I want to run some commands on remote machine. I use julia to manage my commands. To avoid blocking in main threads, I run the commands in a spawned thread.For example:
cmd = """
sleep 3
echo Hello
"""
Threads.@spawn begin
run(`ssh user@remoteip "$cmd"`)
end
However, it seems that the ssh still blocked the main thread. I can do nothing in the REPL. But if I run those commands locally, the main thread won’t be blocked:
Threads.@spawn begin
run(`bash -c "$cmd")
end
In my julia REPL,Threads.nthreads() equals 16. And I’m sure the code above almost runs in different thread everytime. Is the ssh’s internet io communication blocked the main thread? Or have I missed any thing?
I know, we can use Distributed’s functions. Could we just use Threads package to realize my needs? Thanks.
Thanks, I confirmed again. Your code works for me too. The test string outputs normally. But have you found that when you run the code, the REPL stuck? I think I must misunderstand the phenomenon, so, it’s not the main thread is blocked, it’s just the REPL blocked. Sorry for the inappropriate description.
No, the REPL isn’t stuck - I can execute commands as usual. Is it actually blocked, i.e. entering things doesn’t show up? Can you run commands? Do you maybe need the -n flag to ssh, so that ssh doesn’t try to highjack your stdin? On my machine I have this:
-n Redirects stdin from /dev/null (actually, prevents reading from stdin). This must be used when ssh
is run in the background. A common trick is to use this to run X11 programs on a remote machine.
For example, ssh -n shadows.cs.hut.fi emacs & will start an emacs on shadows.cs.hut.fi, and the X11
connection will be automatically forwarded over an encrypted channel. The ssh program will be put
in the background. (This does not work if ssh needs to ask for a password or passphrase; see also
the -f option.) Refer to the description of StdinNull in ssh_config(5) for details.
If you want to capture the output of that ssh command in julia, you can use read(`ssh ...`) by the way.