gpt4 book ai didi

webserver - 了解 Racket Web 框架中的发送/挂起/调度

转载 作者:行者123 更新时间:2023-12-01 02:11:17 25 4
gpt4 key购买 nike

我试图理解 Racket Web 框架教程 http://docs.racket-lang.org/continue/ 中与发送/挂起/调度代码相关的示例.

为方便起见,这里是让我感到困惑的代码:

#lang web-server/insta
; start: request -> response
(define (start request)
(show-counter 0 request))

; show-counter: number request -> doesn't return
; Displays a number that's hyperlinked: when the link is pressed,
; returns a new page with the incremented number.
(define (show-counter n request)
(local [(define (response-generator embed/url)
(response/xexpr
`(html (head (title "Counting example"))
(body
(a ((href ,(embed/url next-number-handler)))
,(number->string n))))))

(define (next-number-handler request)
(show-counter (+ n 1) request))]

(send/suspend/dispatch response-generator)))

我有两个问题:

(最重要的) embed/url 在哪里定义?我看不到它是在这段代码中定义的,但我对延续的理解是基本的,所以我可能遗漏了一些东西。

本地化的目的是什么?我可以删除它,代码似乎工作得一样。

最佳答案

函数 embed/url 是响应生成器的参数:

(define (response-generator embed/url) ...)

(send/suspend/dispatch response-generator)被评估时,会发生以下情况:
1. a procedure `p` given a "continuation" (here next-number-handler)" 
generates an url
2. the function `response-generator` is called with `p` as argument.
3. the page `(html ... ,(embed-url next-number-handler)) is generated
(note: (embed-url next-number-handler) calls `p` and the resulting url is inserted into the page)

4. [send] the page is sent to the client
5. [suspend] the server suspends the program
6. [dispatch] receives an request generated by clicking link whose url were
generated in 3. The handler associated to the url
(here next-number-handler) is looked up, and the handler
is called.

虽然不是使用 send/suspend/dispatch 所必需的这是一个草图:
(define (send/suspend/dispatch response-generator )
(let/ec escape
(define (callback->url callback)
(let/ec return-url
(escape (callback (send/suspend return-url)))))
(send/back (response-generator callback->url))))

关于webserver - 了解 Racket Web 框架中的发送/挂起/调度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29099375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com