exbctel
exbctel

Reputation: 195

Why is my clojure shell result not like what works in python?

When working in the python repl I often need to edit multiline code.

So I use import os then os.system("notepad npad.py")

In clojure I first run (use '[clojure.java.shell :only [sh]])

Then I run (sh "notepad" "jpad.clj")

This starts notepad but not in a useful way because the clojure repl now hangs. In other words, until I close notepad I cannot enter code in the repl and I want to keep both open.

I know I can easily open notepad without clojure so it is no big deal. However, is there a way for clojure to start an external process without hanging?

Upvotes: 3

Views: 194

Answers (2)

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91587

future is a convenient way to leave work running in the background, and it is easy to check up on the process later.

user> (def notepad-process (future (sh "emacs" "jpad.clj")))
#'user/notepad-process

Edit the file for a while, then check on the process later if you want its exit code:

user> @notepad-process
{:exit 0, :out "", :err ""} 

Upvotes: 5

Ben
Ben

Reputation: 6059

It sounds like you want sh to return immediately instead of waiting for notepad's exit code. How about writing a sh! macro or somesuch that runs the original sh command on a new Thread? If you're only using this as a convenience in the REPL, it would be entirely unproblematic.

EDIT

Arthur's answer is better and more Clojurian - go with that.

Upvotes: 1

Related Questions