Attempt At Futures
Spoiler alert: I'm really really new to this language so don't expect quality code In an attempt to see how futures work I'm trying to make code that does the following: 1. Take a list of sites 2. Loop through the sites and retrieve the HEAD content via a future for each individual site 3. In the main loop once the futures have been created print the HEAD content as each future completes Now I have this code which I'm getting stuck at: (ns cjr-http-test.core (:require [clj-http.client :as client])) (defn get-head-response-for-sites [sites] (map (fn [site] (future (client/head site))) sites)) (doseq [head-data (get-head-response-for-sites '("http://www.google.com; "http://www.yahoo.com; "http://www.bing.com;))] (println (deref head-data))) (shutdown-agents) (Please note I know that the list of sites I'd normally expect to be something from a DB/text file. I'm just trying to get it working without adding extra things to think about) So get-head-response-for-sites is where I'm trying to do #2. It gets me a list of futures that I can use. Where I'm having trouble is that the current println line, which is where I'm trying to deal with #3, blocks due to deref-ing so it's basically not really all that different than if I did it non threading. What I (think) I need is something that keeps looping through all the futures, checking their status, and println’ing the result when something has come back. This will be repeated until all the futures are done. The reason I want this is that for example if the first site takes 3 minutes to respond I want the other two sites to print their HEAD content as soon as it’s retrieved. Here's what I'm trying to figure out in order of importance: 1. How do I get a constant loop through the futures, println'ing the HEAD content as they finish, until all futures are finished? 2. Is there a better way to structure this? 3. Is there something in Clojure/contrib that's better suited for this? 4. Is there a 3rd party library better suited for this? Thanks for any and all response. Once again I apologize for the not so pro code but some code is better than nothing I hope. - Chris White ( @cwgem ) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Advice getting started with concurrency and parallelism in Clojure
On Tuesday, April 5, 2016 at 6:51:59 PM UTC-7, tbc++ wrote: > > If it all seems confusing, do not despair, there's two things that will > handle the vast majority of the use cases you may have: > > 1) `future` - spawns a thread that runs the body of the future ( > https://clojuredocs.org/clojure.core/future) > 2) `atom` and `swap!` - Used to store data that needs to be shared between > threads and updated concurrently ( > https://clojuredocs.org/clojure.core/atom) these are built on top of CAS, > which itself is foundation upon which most of concurrent programming is > built. (https://en.wikipedia.org/wiki/Compare-and-swap) > > Those two primitives alone will handle 90% of the use cases you will run > into as a new clojure developer. The rest of the stuff (agents, thread > pools, refs, vars, cps/core.async) can all come in time, but you will use > them much less often than threads and atoms. So read up on those two and > feel free to come back with any questions you may have. > > Okay I've been taking a look at these docs and some articles around them. I think where most of my confusion arises from expecting to see some form of threading or process spawning. Instead I see something like: user=> (def a (atom #{}))#'user/a user=>(swap! a conj :tag)#{:tag} user=> @a#{:tag} Which is showing a mutable value in a language that (from what I understand) values immutability. The thing that's throwing me off is that none of the examples I'm finding actually shows threaded code. I guess what I'm looking for is that kind of example to see how it all fits together. > Timothy > > > On Tue, Apr 5, 2016 at 7:24 PM, Chris White <cwpr...@live.com > > wrote: > >> I was doing some reading of code recently to help me get up to speed with >> Clojure. One of the libraries I randomly came across dealt with parallelism >> and I had a hard time following along with it. To try and wrap my head >> around things I did a quick search and found this article: >> >> >> http://www.thattommyhall.com/2014/02/24/concurrency-and-parallelism-in-clojure/ >> >> I'm not sure how authoritative this is based on my current experience, >> but needless to say I was a bit overwhelmed. That said is there any sort of >> introductory material that list members have used to help get them into how >> Clojure deals with concurrency and parallelism? I also don't mind anything >> that's not specifically using Clojure but will at least help me understand >> the concepts behind how Clojure does it. Thanks again for any and all help! >> >> - Chris White (@cwgem) >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clo...@googlegroups.com >> >> Note that posts from new members are moderated - please be patient with >> your first post. >> To unsubscribe from this group, send email to >> clojure+u...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en >> --- >> You received this message because you are subscribed to the Google Groups >> "Clojure" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to clojure+u...@googlegroups.com . >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > “One of the main causes of the fall of the Roman Empire was that–lacking > zero–they had no way to indicate successful termination of their C > programs.” > (Robert Firth) > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Advice getting started with concurrency and parallelism in Clojure
I was doing some reading of code recently to help me get up to speed with Clojure. One of the libraries I randomly came across dealt with parallelism and I had a hard time following along with it. To try and wrap my head around things I did a quick search and found this article: http://www.thattommyhall.com/2014/02/24/concurrency-and-parallelism-in-clojure/ I'm not sure how authoritative this is based on my current experience, but needless to say I was a bit overwhelmed. That said is there any sort of introductory material that list members have used to help get them into how Clojure deals with concurrency and parallelism? I also don't mind anything that's not specifically using Clojure but will at least help me understand the concepts behind how Clojure does it. Thanks again for any and all help! - Chris White (@cwgem) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Downloading Clojure + Contrib Source
On Monday, April 4, 2016 at 5:31:21 AM UTC-7, Stuart Sierra wrote: > > Hi Chris, > > No, there is no single command I know of to automatically download all of > the contrib libraries. It's something you could do with a shell script, I > expect. > > But you probably don't need to do that if you're just interested in > learning Clojure. > > 'contrib' just means things contributed to “Clojure” as an organization, > governed by the same Contributor Agreement and patch process. The 'contrib' > libraries have many different authors, release cycles, and degrees of > maturity. They are not necessarily meant to be a “standard library,” > although some of them are more-or-less standard. > What is Clojure Contrib? > <http://www.google.com/url?q=http%3A%2F%2Fdev.clojure.org%2Fpages%2Fviewpage.action%3FpageId%3D5767464=D=1=AFQjCNENnUe1PqRVYGCHCsPx-23G5mFkRw> > Okay I guess the main thing I'm looking for is something that's been (outside of clojure/clojure of course) written by the people who made the language. Also it gives me an authoritative source for "how is this supposed to work" in case the docs don't have something explained. > > > If you're interested in learning about Clojure by reading source code, you > might look for popular Clojure libraries (not necessarily 'contrib') on > GitHub or sites like Clojure Toolbox <http://www.clojure-toolbox.com/>. > Most Clojure books and online tutorials will also point you in the > direction of popular libraries. > > –S > > I'm assuming Clojars <https://clojars.org/> seems to be a good place to look around for such code. I'll take a look at Clojure Toolbox as well. Thanks for the response! - Chris > > On Monday, April 4, 2016 at 8:16:45 AM UTC-4, Chris White wrote: >> >> I'm currently in the process of learning Clojure and would like to look >> around the source code to see how it's laid out. Unfortunately I'm having >> trouble finding an easy way to download not only the Clojure source code, >> but also the contrib packages. Currently it looks like I have to download >> everything in the clojure GitHub organization. Is there some kind of script >> used by developers to pull everything down into a single folder? Is there a >> way that all the contrib packages can be released as a single source >> tarball? Thanks ahead of time for any responses. >> >> - Chris White >> > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Downloading Clojure + Contrib Source
I'm currently in the process of learning Clojure and would like to look around the source code to see how it's laid out. Unfortunately I'm having trouble finding an easy way to download not only the Clojure source code, but also the contrib packages. Currently it looks like I have to download everything in the clojure GitHub organization. Is there some kind of script used by developers to pull everything down into a single folder? Is there a way that all the contrib packages can be released as a single source tarball? Thanks ahead of time for any responses. - Chris White -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.