kyp0717
2021-6-2 10:52:53

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.


yfangzhe
2021-6-2 10:57:19

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.


samth
2021-6-2 12:12:43

@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/


kyp0717
2021-6-2 13:16:09

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


kyp0717
2021-6-2 13:18:38

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.


kyp0717
2021-6-2 13:18:49

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


samth
2021-6-2 13:23:29

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


sorawee
2021-6-2 13:26:29

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.


kyp0717
2021-6-2 13:33:01

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.


hazel
2021-6-2 19:12:52

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


badkins
2021-6-2 21:26:11

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


badkins
2021-6-2 21:27:23

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\"")


jbclements
2021-6-2 21:27:46

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


badkins
2021-6-2 21:28:01

No, is it better?


badkins
2021-6-2 21:29:26

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


blachance
2021-6-2 22:00:19

@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{&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;foo&gt; &lt;bar&gt;asdf&lt;/bar&gt; &lt;/foo&gt;})) (and the p-i part in your error message at least looks like part of the the parsed preamble)


badkins
2021-6-2 22:03:20

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.


badkins
2021-6-2 22:08:33

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


badkins
2021-6-2 22:09:22

But if I then use (xml-&gt;xexpr (read-xml ...)) I get an error.


blachance
2021-6-2 22:09:55

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


badkins
2021-6-2 22:10:18

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


badkins
2021-6-2 22:11:09

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 :(


blachance
2021-6-2 22:11:20

d’oh


badkins
2021-6-2 22:15:37

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


badkins
2021-6-2 22:15:59

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


badkins
2021-6-2 22:16:39

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


blachance
2021-6-2 22:20:29

hurray! :tada:


greg
2021-6-3 00:44:25

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.


greg
2021-6-3 00:44:56

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.


greg
2021-6-3 00:45:09

@hazel ^


greg
2021-6-3 00:47:59

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.


hazel
2021-6-3 01:37:53

@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


hazel
2021-6-3 01:38:01

changing it fixed the issue