This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-09-17
Channels
- # 100-days-of-code (4)
- # announcements (4)
- # aws (2)
- # beginners (88)
- # cider (1)
- # cljsrn (9)
- # clojure (126)
- # clojure-conj (4)
- # clojure-dev (8)
- # clojure-greece (1)
- # clojure-italy (37)
- # clojure-nl (3)
- # clojure-spec (13)
- # clojure-uk (91)
- # clojurescript (392)
- # clojurewerkz (1)
- # clojutre (10)
- # core-async (6)
- # cursive (5)
- # data-science (1)
- # datomic (41)
- # emacs (21)
- # events (1)
- # figwheel-main (52)
- # fulcro (2)
- # hyperfiddle (4)
- # jobs (3)
- # jobs-discuss (9)
- # luminus (3)
- # lumo (9)
- # mount (1)
- # nyc (1)
- # off-topic (73)
- # other-languages (6)
- # pedestal (8)
- # portkey (2)
- # re-frame (9)
- # reagent (8)
- # rum (17)
- # shadow-cljs (38)
- # sql (19)
- # tools-deps (18)
- # yada (4)
I’m new to clojure and I’m not familiar with all the asynchronous mechanisms so I’m looking for a little guidance. I have a jdbc query that could take minutes or hours. How do I construct an asynchronous query mechanism? Specifically, I want the function to return immediately and perhaps a callback function for processing the results after the query returns. Is there a function I could call which would take a query as the first argument, execute it on a thread and return and a callback function which will be called when the results of the query return. For example,
(do-async (jdbc/query db-spec query) :callback (fn [response] (process-response response))))
I’ve been reading the docs for core.async but can’t seem to put it all together in a cohesive way. Any pointers to guides, examples, blog posts or libraries would be appreciated. Thanks!if you are coming from a js background, async there is usually synonymous with multiple threads of execution which are achieved by slicing things in to multiple tasks that executed one at a time on a single real thread
the jvm is a multithreaded runtime, so if you want multiple threads of execution, you can just have them
@hiredman Thanks for your help. I’ll read up on thread/future. I have a Java UI background so I’m thinking of a mechanism like SwingWorker
@hiredman I’ve been reading a bit about future and I’m not clear how I can process the results asynchronously. It appears that we must dereference the result (channel?) in order to get the query results and that dereference will block until the function passed into the future is complete. I just want to define a callback function which will be called with the query results when the future task completes. Is there any guidance on that use case?
when you give it stuff to run, it is immediately run on another thread in a threadpool
I’m not getting the results I’m looking for. It appears that I’ll have to poll the future to determine when it is done and then I can dereference it to extract the value. How can I avoid polling and have the results passed to a function automatically when the future is done?
On a note: JDBC is not asynchronous. The most you can do with it is execute on a different thread so you're original calling thread isn't blocked, which is what future
does for you.
If you want to run it on a separate thread, and then have a function process the result in the future without polling, you probably want to look into either core.async
, manifold
, or the simple method that @hiredman suggested of:
(future (my-final-result-fn (my-long-running-query)))
I would avoid the two libraries I mentioned until you know you actually need something more complex than the future
version