Fork me on GitHub
#beginners
<
2018-04-01
>
weihua01:04:33

thanks for the explanation, Sean!

lilactown03:04:16

i have a dumb question

lilactown03:04:55

I’m currently using clojure.core.server to start a socket server that I’m sending some messages back and forth from

lilactown03:04:30

works great in my CIDER session. however, when I uberjar it and run it… it just closes after initializing everything

lilactown03:04:18

I’m guessing that the socket server was anticipated to be used alongside an already-long-running process. is there a simple way to keep my clojure process alive while the meat and potatoes of my app is just using the socket server?

lilactown03:04:50

or should I switch to something that’s meant for more what I’m doing, like aleph or clj-sockets?

lilactown03:04:17

or should I just add an http server for giggles lol

ackerleytng10:04:17

when i do lein ring server-headless, i should have an nrepl, right?

ackerleytng10:04:25

how do i connect to that nrepl from emacs?

ackerleytng10:04:49

I found out that in project.clj, i need

:ring {:nrepl {:start? true}}

ackerleytng10:04:00

and then i can do C-c M-c to connect to the nrepl

ackerleytng10:04:22

I'm getting this now:

WARNING: CIDER's version (0.17.0-snapshot) does not match cider-nrepl's version (nil). Things will break!
what should i do?

niconico11:04:41

Hi, having trouble with promises, lazy seqs, and side effects. I wrote this function where 'op' is basically an http-kit call that returns a promise. I believe that the function fails to run the operations concurrently (because the invocation of for returns a lazy seq, only the first one is started, and the second one is started only when @resp of the first one has finished waiting). How can I force the execution of side effects inside the let block? Thanks

(defn mp-update-user-profile
  " Takes zero or more [op user-id args] triples, performs the operations concurrently, and awaits all results"
  [& actions]
  (let [futures (for [[op user-id args] (partition 3 actions)]
                   (op mp-token user-id {:ip 0 :ignore_time true} args))]
    (doseq [resp futures]
      (timbre/debug @resp))))

niconico11:04:34

ok, because I know and have previously used doseq I was expecting some macro that does exactly what I want. I just realized I can just invoke doall on the result of for to force the execution of side effects.

danielo51511:04:45

Your problem was that you want to run them in parallel but they were running in sequence or that they didn’t run at all?

niconico11:04:34

wanted to run them in parallel but I believe they were running in sequence (I have no proof, and everything is running so fast that it is difficult to perceive the difference)

niconico12:04:04

OK I've just added some logging inside the for block to find out when the operation is first invoked and now I have proof that before I added doall things were happening in sequence, and after I added the doall I see it's concurrent. Thanks for your help @rdanielo

danielo51512:04:45

Actually I didn't helped in any way apart from morally 😄

danielo51512:04:59

But I would like to see your final solution

niconico13:04:59

I always call the function with 3 commands, which means I'm expecting 3 requests in parallel. First version is with for:

(defn mp-update-user-profile
  " Takes zero or more [op user-id args] triples, performs the operations concurrently, and awaits all results.
  Awaiting slows things down but ensures mixpanel is notified before we exit the program."
  [& actions]
  (let [futures (for [[op user-id args] (partition 3 actions)]
                  (do
                    (timbre/debug "Before request")
                    (op mp-token user-id {:ip 0 :ignore_time true} args)))]
    (doseq [resp futures]
      (timbre/debug "got response" (:status @resp)))))
The output shows requests happening sequentially:
18-04-01 13:31:57 point DEBUG [moe.core:15] - Before request
18-04-01 13:31:57 point DEBUG [moe.core:18] - got response 200
18-04-01 13:31:57 point DEBUG [moe.core:15] - Before request
18-04-01 13:31:57 point DEBUG [moe.core:18] - got response 200
18-04-01 13:31:57 point DEBUG [moe.core:15] - Before request
18-04-01 13:31:57 point DEBUG [moe.core:18] - got response 200
-------------- Second version is with a doall around the for:
(defn mp-update-user-profile
  " Takes zero or more [op user-id args] triples, performs the operations concurrently, and awaits all results.
  Awaiting slows things down but ensures mixpanel is notified before we exit the program."
  [& actions]
  (let [futures (doall
                  (for [[op user-id args] (partition 3 actions)]
                    (do
                      (timbre/debug "Before request")
                      (op mp-token user-id {:ip 0 :ignore_time true} args))))]
    (doseq [resp futures]
      (timbre/debug "got response" (:status @resp)))))
The output shows all requests starting before the first response arrives:
18-04-01 13:34:31 point DEBUG [moe.core:16] - Before request
18-04-01 13:34:31 point DEBUG [moe.core:16] - Before request
18-04-01 13:34:31 point DEBUG [moe.core:16] - Before request
18-04-01 13:34:31 point DEBUG [moe.core:19] - got response 200
18-04-01 13:34:31 point DEBUG [moe.core:19] - got response 200
18-04-01 13:34:31 point DEBUG [moe.core:19] - got response 200

sarna15:04:43

hey, I want to implement a queue for songs in my bot. basically I want to wait until a song stops to play another one. how would I implement this? I don't want to sleep for the duration, because I wouldn't be able to add songs to the queue when a song is playing

akiroz19:04:54

@mailmeupinside just putting this out there but I think you might need to provide more context before anyone can say anything useful... some sample code might help.

Josh Horwitz21:04:12

Update an an atom that holds the state of playing or not playing and change it at the end of song to false which picks up the next song of the queue and flips it to true, and so on?

sarna23:04:06

@akiroz the thing is I don't have any code yet :^) I'm struggling to implement it in another language atm, and thought I'd maybe switch to Clojure if there was an obvious solution for this problem in Clojure

sarna23:04:52

@joshua.d.horwitz there's no "song ended" event, I only know when it started playing and the duration. I feel I need async for that :thinking_face:

leongrapenthin23:04:57

@mailmeupinside you can wait for a timeout channel

sarna11:04:39

sounds good, thank you!

leongrapenthin23:04:55

(But you should reconsider having at least a virtual stop evt because it might stop playing for a variety of other reasons,)

👍 1