This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-03-06
Channels
- # adventofcode (1)
- # announcements (5)
- # architecture (4)
- # babashka (35)
- # beginners (28)
- # calva (4)
- # cider (9)
- # clerk (27)
- # clj-kondo (11)
- # clojure (58)
- # clojure-conj (3)
- # clojure-europe (71)
- # clojure-gamedev (2)
- # clojure-nl (3)
- # clojure-uk (2)
- # clojurescript (49)
- # conjure (2)
- # cursive (2)
- # emacs (4)
- # fulcro (1)
- # honeysql (2)
- # hyperfiddle (9)
- # jobs (3)
- # jobs-discuss (13)
- # leiningen (9)
- # membrane (14)
- # missionary (1)
- # off-topic (27)
- # pedestal (1)
- # polylith (5)
- # reagent (12)
- # releases (2)
- # remote-jobs (4)
- # shadow-cljs (83)
- # sql (5)
- # squint (13)
- # tools-build (13)
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")}))
You can just pass (io/file "/tmp/foo")
to :out
but I'll have a look why this doesn't work
Done
(context: I'm in a situation where I already have FileWriter
s to work with)
@UCFG3SDFV It's interesting. When I try this:
(io/copy (io/file "README.md") (java.io.FileWriter. "/tmp/foo.txt"))
@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
@UCFG3SDFV This makes it work for me:
(with-open [fw (java.io.FileWriter. "/tmp/foo.txt")] (io/copy (io/file "README.md") fw))
This also seems to work for me:
(with-open [fw (java.io.FileWriter. "/tmp/foo.txt")]
@(babashka.process/process {:out fw}
"echo" "foo"
))
It looks like something is not quite right with flushing?
Although I didn't have any issue with FileOutpuStream
for instance.
something like this, I think the rule is that whoever creates (owns) the writer, should close it
Yep looks like flushing indeed. I somehow expected it to be done automatically.
Solved by flushing manually using :exit-fn
👍
Sensible but perhaps not quite suitable for my use case
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.
Quick repro case
bb -e '(.join (Thread/startVirtualThread (fn [] nil)))'
# Type: java.lang.IllegalArgumentException
# Message: No matching method startVirtualThread found taking 1 args
@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)
So in short:
(import 'java.util.concurrent.Executors)
(defn run-task
[f]
(let [executor (Executors/newVirtualThreadPerTaskExecutor)]
(.submit executor f)))
there are various other ways to start a virtual thread too. if you can find more of them, feel free to list them
Sorry about the delay, the issue is here. https://github.com/babashka/babashka/issues/1510
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.