Fork me on GitHub
#babashka
<
2023-03-06
>
Adam Helins10:03:01

Am I doing it wrong? The given file is still empty, I can't get process to work with FileWriter as :out or :err (which should be compatible with ):

(deref (babashka.process/process ["echo" "foo"]
                                 {:out (java.io.FileWriter. "/tmp/foo")}))

borkdude10:03:16

You can just pass (io/file "/tmp/foo") to :out but I'll have a look why this doesn't work

borkdude11:03:49

no I don't ;)

borkdude11:03:37

issue welcome

Adam Helins11:03:31

Done (context: I'm in a situation where I already have FileWriters to work with)

borkdude12:03:20

@UCFG3SDFV It's interesting. When I try this:

(io/copy (io/file "README.md") (java.io.FileWriter. "/tmp/foo.txt"))

borkdude12:03:33

I don't see the whole output of README.md and up in /tmp/foo.txt

borkdude12:03:11

@UCFG3SDFV So I'm beginning to think the issue isn't maybe with process but more upstream or perhaps FileWriter isn't suited for io/copy

borkdude12:03:45

@UCFG3SDFV This makes it work for me:

(with-open [fw (java.io.FileWriter. "/tmp/foo.txt")] (io/copy (io/file "README.md") fw))

borkdude12:03:54

This also seems to work for me:

(with-open [fw (java.io.FileWriter. "/tmp/foo.txt")]
  @(babashka.process/process {:out fw}
                             "echo" "foo"
                             ))

Adam Helins12:03:06

It looks like something is not quite right with flushing? Although I didn't have any issue with FileOutpuStream for instance.

borkdude13:03:51

something like this, I think the rule is that whoever creates (owns) the writer, should close it

Adam Helins13:03:35

Yep looks like flushing indeed. I somehow expected it to be done automatically. Solved by flushing manually using :exit-fn đź‘Ť

borkdude13:03:31

why didn't you use with-open?

borkdude13:03:27

I think that is a better solution since you will also release any file resources

Adam Helins13:03:10

Sensible but perhaps not quite suitable for my use case

borkdude13:03:27

ok, whatever works best in your context :)

Adam Helins13:03:58

Thanks by the way, helpful as always 🙂

🙏 2
Gaspard22:03:14

Hello, I was testing virtual threads with bb and realized that the Thread/startVirtualThread function is not supported. Is it something worth adding to babashka, even though the whole “virtual thread” is still in preview? Happy to draft a PR if that’s the case.

Gaspard22:03:30

Quick repro case

bb -e '(.join (Thread/startVirtualThread (fn [] nil)))'
# Type:     java.lang.IllegalArgumentException
# Message:  No matching method startVirtualThread found taking 1 args

borkdude22:03:46

@U03P5N53YFJ I noticed that too. Issue welcome about this. You can however do this via an executor, which is probably the only way that's been tested right now.

(import 'java.util.concurrent.Executors)
(import 'java.util.concurrent.Future)

(defn loads-of-tasks
      [concurrency]
      (let [executor (Executors/newVirtualThreadPerTaskExecutor)
        ;; executor   (Executors/newFixedThreadPool concurrency)
            tasks (mapv #(fn []
                           (Thread/sleep 1000)
                           %)
                        (range concurrency))
            start-time (System/currentTimeMillis)
            sum (->> (.invokeAll executor tasks)
                     (map #(.get ^Future %))
                     (reduce +))
            end-time (System/currentTimeMillis)]
        (println "Blazingly Fast!"
                 {:sum sum
                  :time-ms (- end-time start-time)})))

(loads-of-tasks 100000)

borkdude22:03:35

So in short:

(import 'java.util.concurrent.Executors)

(defn run-task
  [f]
  (let [executor (Executors/newVirtualThreadPerTaskExecutor)]
    (.submit executor f)))

Gaspard22:03:16

Thanks! That’s a pretty good work around.

Gaspard22:03:22

Still, I’ll open an issue about it as well

borkdude22:03:57

there are various other ways to start a virtual thread too. if you can find more of them, feel free to list them

đź‘Ť 2
Gaspard18:03:34

Sorry about the delay, the issue is here. https://github.com/babashka/babashka/issues/1510

Gaspard18:03:35

There are also a bunch of missing methods on the new Thread.Builder interface (and Thread/ofPlatform is not implemented either) but I thought this deserved its own issue.

borkdude18:03:01

I'd say put them also in this issue, then I can address them in one go

borkdude18:03:19

with examples please, so I can make tests out of them

borkdude18:03:43

like you've already done đź‘Ź

Gaspard18:03:10

Sure thing, it’ll take a bit of time to get the examples for the Thread.Builder but I’ll add it asap.

borkdude18:03:07

no hurry, I won't go this before tomorrow