
Hello. I need help implementing async http request in Racket. Can anyone share code or point me in the right direction? By the way, I have already read through 18 Concurrency and Synchronization
section in Racket manual.
What I am trying to do is performed http get request in a single thread like it is done in NodeJS or Python Async. For example, I like to request stock prices for several stocks by generating multiple concurrent requests within a single thread.

You can read <https://docs.racket-lang.org/continue/index.html|Continue: Web Applications in Racket> and <https://docs.racket-lang.org/more/index.html|More: Systems Programming with Racket> from official documents.

@kyp0717 You probably want to (a) use a thread
and (b) use http-easy
to make a request: https://docs.racket-lang.org/http-easy/

Thanks @samth and @yfangzhe. I believe thread
will probably do the trick for me.

One of the feature that I am seeking is non-blocking IO where the programs move on to the next step while the previous step waits for the IO to finish. At some later time, the code can come back to previous step and continue after IO is complete.

But I want to do all of this in a single thread.

@kyp0717 in Racket, IO is non-blocking which is why you can run multiple threads while other threads are waiting for IO to finish.

Also, Racket’s thread
is not OS thread, FYI. In case you said “single thread” because you don’t want to use multiple OS threads.

OK. Thanks. I am convince thread
is the correct package for me now. I believe that racket thread
is probably as light-weight or the same as an async/await
process in Python Async
package. I definitely don’t want to spawn multiple threads for IO related stuff where waiting for http request to come back is the bottleneck.

this happens when I try to launch a REPL with racket-mode — what gives?

I’m using the xml
package to parse some xml, and it seems to have a problem with the optional preamble which looks like: <?xml version="1.0" encoding="UTF-8"?>
e.g. <?xml version="1.0" encoding="UTF-8"?>
<foo>
<bar>asdf</bar>
</foo>
I couldn’t find any parameters to tweak to placate it. Am I missing something?

Error is: read-xml: parse-error: expected root element - received (p-i (location 1 0 1) (location 1 38 39) 'xml "version=\"1.0\" encoding=\"UTF-8\"")

not really an answer, but have you considered using the sxml package?

No, is it better?

I suppose I’ll just strip off the preamble manually.

@badkins do you have a complete example of a failing program? for some reason this works on my machine #lang at-exp racket
(require xml)
(read-xml
(open-input-string
@~a{<?xml version="1.0" encoding="UTF-8"?>
<foo>
<bar>asdf</bar>
</foo>}))
(and the p-i part in your error message at least looks like part of the the parsed preamble)

Thanks for trying that @blachance - let me get a small snippet to reproduce it. I’m running Racket v8.1.0.6 [cs]
, but I doubt it’s sensitive to versions.

Ah… it does appear that read-xml
does not crash, but read-xml/element
does.

But if I then use (xml->xexpr (read-xml ...))
I get an error.

interesting! maybe it’s the xexpr representation that can’t handle preambles?

Maybe - I’ll keep digging. Good to know that read-xml
is solid :)

The documentation for this 3rd party service did not show a preamble, so I coded/tested according to their docs, and everything was fine, but in live testing, the preamble was stuck on there :(

d’oh

Got it! (xml->xexpr
(document-element
(read-xml
(open-input-string login-validation-str))))
Thanks! You got me on the right track :)

Much better than my hack of using string-replace
to strip off the prolog :)

The xml
package is vindicated :) I was simply using it incorrectly for the given input.

hurray! :tada:

Racket Mode’s Emacs Lisp front end starts a Racket program which is Racket Mode’s back end. The program expects two command-line arguments, but is getting three. I’m not sure why. Admittedly the first one has a space in it, but, in my experience that hasn’t mattered. Maybe it’s due to your OS, shell, and/or version of Emacs.

If you M-x racket-bug-report
and submit it there might be more details that help me. At least we could discuss more there to figure out.

@hazel ^

p.s. I suppose there’s no good reason it must supply (auth 1234)
; I’m not sure why I didn’t just have it supply say --auth 1234
or even just 1234
. I could also look into making such a change and we can see if that fixes the issue you’re having.

@greg it was my fault. due to my running NixOS, I have wrapper scripts that set $LD_LIBRARY_PATH
before running the Racket I build manually from Git. however, in those wrapper scripts, I was using $@
instead of "$@"
so something was going awry

changing it fixed the issue