简体   繁体   中英

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?

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 ""} 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM