Nimlang:异步程序无法编译
Nimlang: Async program does not compile
我正在尝试编写一个 HTTP 服务器来发送 HTTP 请求和 returns 内容到客户端。
代码如下:
import asynchttpserver, asyncdispatch
import httpClient
let client = newHttpClient()
var server = newAsyncHttpServer()
proc cb(req: Request) {.async.} =
let content = client.getContent("http://google.com")
await req.respond(Http200, content)
waitFor server.serve(Port(8080), cb)
但是,我得到以下编译错误信息(nim v1.0.0):
Error: type mismatch: got <AsyncHttpServer, Port, proc (req: Request): Future[system.void]{.locks: <unknown>.}>
but expected one of:
proc serve(server: AsyncHttpServer; port: Port;
callback: proc (request: Request): Future[void] {.closure, gcsafe.};
address = ""): owned(Future[void])
first type mismatch at position: 3
required type for callback: proc (request: Request): Future[system.void]{.closure, gcsafe.}
but expression 'cb' is of type: proc (req: Request): Future[system.void]{.locks: <unknown>.}
This expression is not GC-safe. Annotate the proc with {.gcsafe.} to get extended error information.
expression: serve(server, Port(8080), cb)
服务函数需要另一个表达式,但不知道如何修复它。
令人惊讶的是,当我从服务器回调 "cb" 中删除 HTTP 请求时,代码编译得非常好。这是否意味着服务函数需要不同的回调表达式,具体取决于回调主体?
好的,问题是 HttpClient 是一个全局变量,用于回调函数 "cb"。因此,回调函数不是 GC 安全的。
所以在回调函数中实例化 HttpClient 就足够了:
import asynchttpserver, asyncdispatch
import httpClient
var server = newAsyncHttpServer()
proc cb(req: Request) {.async.} =
let client = newHttpClient()
let content = client.getContent("https://google.com")
await req.respond(Http200, content)
waitFor server.serve(Port(8080), cb)
我正在尝试编写一个 HTTP 服务器来发送 HTTP 请求和 returns 内容到客户端。
代码如下:
import asynchttpserver, asyncdispatch
import httpClient
let client = newHttpClient()
var server = newAsyncHttpServer()
proc cb(req: Request) {.async.} =
let content = client.getContent("http://google.com")
await req.respond(Http200, content)
waitFor server.serve(Port(8080), cb)
但是,我得到以下编译错误信息(nim v1.0.0):
Error: type mismatch: got <AsyncHttpServer, Port, proc (req: Request): Future[system.void]{.locks: <unknown>.}>
but expected one of:
proc serve(server: AsyncHttpServer; port: Port;
callback: proc (request: Request): Future[void] {.closure, gcsafe.};
address = ""): owned(Future[void])
first type mismatch at position: 3
required type for callback: proc (request: Request): Future[system.void]{.closure, gcsafe.}
but expression 'cb' is of type: proc (req: Request): Future[system.void]{.locks: <unknown>.}
This expression is not GC-safe. Annotate the proc with {.gcsafe.} to get extended error information.
expression: serve(server, Port(8080), cb)
服务函数需要另一个表达式,但不知道如何修复它。 令人惊讶的是,当我从服务器回调 "cb" 中删除 HTTP 请求时,代码编译得非常好。这是否意味着服务函数需要不同的回调表达式,具体取决于回调主体?
好的,问题是 HttpClient 是一个全局变量,用于回调函数 "cb"。因此,回调函数不是 GC 安全的。
所以在回调函数中实例化 HttpClient 就足够了:
import asynchttpserver, asyncdispatch
import httpClient
var server = newAsyncHttpServer()
proc cb(req: Request) {.async.} =
let client = newHttpClient()
let content = client.getContent("https://google.com")
await req.respond(Http200, content)
waitFor server.serve(Port(8080), cb)