我正在尝试从光纤http://vibed.org/api/vibe.core.core/runWorkerTaskH接收数据
“在工作线程中运行新的异步任务,返回任务句柄。”
这是我的代码:
import vibe.vibe;
import std.stdio;
import std.datetime;
import vibe.core.core;
string [] test_urls = ["http://127.0.0.1:8081/hck","http://127.0.0.1:8081/hck2","http://127.0.0.1:8081/hck3"];
struct MyUrl
{
string url;
string status;
}
MyUrl myUrl;
MyUrl [] myUrls;
shared static this()
{
foreach(url;test_urls)
{
myUrl.url = url;
myUrls ~= myUrl;
}
setTimer(2.seconds, toDelegate(&processing), true);
}
void main()
{
listenHTTP(":8080", &handleRequest);
runApplication();
}
void handleRequest(HTTPServerRequest req, HTTPServerResponse res)
{
}
void processing()
{
writeln("processing");
//foreach(url; myelements)
foreach(ref url; myUrls)
{
Task answer = runWorkerTaskH(&getServiceStatus, Task.getThis, url);
writefln("url: %s, status: %s", url.url, url.status);
}
}
void getServiceStatus(Task caller, MyUrl url) { // seems hiden copy accure
requestHTTP(url.url,
(scope req) {
req.method = HTTPMethod.GET;
},
(scope res) {
Json serviseAnswer = parseJsonString(res.bodyReader.readAllUTF8()); // status and message
url.status = serviseAnswer["status"].toString();
caller.send("goodbye");
//url.changeStatus(url.status);
}
);
}
但是运行后出现错误:
Running .\app.exe
[main(----) INF] Listening for requests on http://[::]:8080/
[main(----) INF] Listening for requests on http://0.0.0.0:8080/
processing
url: http://127.0.0.1:8081/hck, status:
url: http://127.0.0.1:8081/hck2, status:
url: http://127.0.0.1:8081/hck3, status:
object.Error@(0): Access Violation
----------------object.Error
@0x00538481(
00x0040B5D5)
: 0x00403B12
Access Violation0x0044D908
----------------
0x00403A1F0x00538481
0x0053B5390x0040B5D5
0xFFFFFFFF0x00403B12
0x7793F30C in RtlCaptureContext
0x0044D908
abnormal program termination
Program exited with code 1
我做错了什么?
我不确定我是否正确调试了应用程序,但是:
logInfo("1111");
caller.send("goodbye");
logInfo("2222");
将打印:
[vibe-7(4hZL) INF] 1111
object.Error@(0): Access Violation
我知道setTimer
不会创建任务,而且我总是会得到null
。但是我不知道如何解决。