Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

clojure

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?

like image 269
exbctel Avatar asked May 07 '26 06:05

exbctel


2 Answers

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 ""} 
like image 88
Arthur Ulfeldt Avatar answered May 08 '26 18:05

Arthur Ulfeldt


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.

like image 27
Ben Avatar answered May 08 '26 19:05

Ben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!