Fork me on GitHub
#babashka
<
2021-06-08
>
pbaille09:06:12

Hello, i'm trying to use babashka.process for a little thing. I would like to be able to launch a process that do not terminate and do something whenever some new output occurs from it. Is there a builtin way to do this? I've try this example from the documentation:

(future
  (loop []
    (spit "log.txt" (str (rand-int 10) "\n") :append true)
    (Thread/sleep 10)
    (recur)))

(pipeline (pb '[tail -f "log.txt"])
          (pb '[cat])
          (pb '[grep "5"] {:out :inherit}))
but I do no see anything printing out, is it intended ?

borkdude09:06:23

@pbaille The readme explains what the problem is: buffering.

borkdude09:06:32

oh now you are using pipeline ok

pbaille09:06:42

sorry I've edited the message

borkdude09:06:19

Yes, so this works:

(require '[babashka.process :refer [pipeline pb]])

(future
  (loop []
    (spit "log.txt" (str (rand-int 10) "\n") :append true)
    (Thread/sleep 10)
    (recur)))


(-> (pipeline (pb '[tail -f "log.txt"])
              (pb '[cat])
              (pb '[grep "5"] {:out :inherit}))
    last
    deref)

pbaille09:06:26

thank you @borkdude, I've just tried this snippet in my editor (intellij cursive) on Java 15 and clojure 1.10.0. Am I supposed to see something printed in the repl ?

borkdude09:06:20

@pbaille :inherit prints directly to stdout (`System/out`)

borkdude09:06:34

Just try (process ["ls"] {:inherit true}) to try it out what happens in your editor

pbaille09:06:36

yes it prints the output

pbaille09:06:39

in the repl

borkdude09:06:19

ok, then that works correctly.

pbaille09:06:56

ok, so I should be able to see some output from your first snippet ?

borkdude09:06:11

yes, I think so

borkdude09:06:22

maybe simplify the example for debugging purposes

pbaille09:06:22

it hangs on the last expression

borkdude09:06:54

you can remove the deref to make it not hang. I just added that to make the bb script not terminate

borkdude09:06:01

let's continue in thread

pbaille09:06:38

I got this but nothing prints

borkdude09:06:25

Can you please try this in a standalone bb script and then execute it from the terminal?

borkdude09:06:47

Or clojure script if you are using this lib on the JVM

pbaille09:06:25

yes I will do this

pbaille09:06:44

it works nicely as a script

borkdude09:06:33

Perhaps try :out *out* in your editor for the last process in the pipeline

borkdude09:06:39

else, I don't know :)

pbaille09:06:46

I've tried this

pbaille09:06:02

but didn't change anything

pbaille09:06:37

ok, thank you for your time @borkdude! I will investigate on my own and let you know if I find something

dabrazhe16:06:31

Can I connect to the bb repl in VSC Calva without manually entering it every time? it does not pick it up from .nrepl-port file

borkdude17:06:17

@dennisa Babashka does not write an .nrepl-port file since that can conflict with a Clojure process.

dabrazhe17:06:50

I can write it myself, just need Calva to pick it up from somewhere, otherwise getting this promt with no port. I have to restart the bb repl rather often as it slows down and it's a chore to type every time 1667 : )

borkdude17:06:05

@dennisa There is a snippet about writing this port file here: https://book.babashka.org/#_nrepl

borkdude17:06:45

(I think this script can be simplified/refactored now since there is babashka.process)

dabrazhe17:06:11

Have someone managed to Calva to pick it up? Or am I in the wrong channe?l :)