You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
question about runWorkerTaskH, since I have a CPU intensive task and need to execute it in thread.
In the example bellow, for each WS connection a new worker task is started. Since the task will perform heavy computations, I expect that each connection will have its own new thread.
But logs and task manager show me a strange behavior, where computations for different WS are done on same thread???
Maybe I do something wrong?
/* * Case 01 * ------- * Worker task is executed always in same worker thread. * * How-To: * - open 4 parallel WS connection to service. * - in each connection send channel id, eg. "ch01", "ch02", ... * - observe logs * - worker thread name is always same*/import vibe.d;
import vibe.vibe;
import vibe.core.core;
import vibe.http.server;
import vibe.http.websockets;
import vibe.http.router;
import vibe.inet.url;
importcore.time;
importcore.thread : Thread;
importstd.conv;
static voidworkerFuncPingPong(Task caller, string channel_id) nothrow {
int counter = 5;
try {
logInfo("WORKER :: thread-id=%s caller=%s channel-id=%s THREAD=%s", thisTid, caller, channel_id, Thread.getThis().name);
while (receiveOnly!string() =="ping"&&--counter) {
logInfo("%s :: %s :: pong=%s", Thread.getThis().name, channel_id, counter);
caller.send("pong");
sleep(2.seconds);
}
caller.send("goodbye");
} catch (Exception e) assert(false, e.msg);
}
classWebsocketService {
@path("/ws") void getWebsocket1(scope WebSocket ws){
logInfo("X> connected=%s, ws=%s code=%s THREAD=%s", ws.connected, &ws, ws.closeCode, Thread.getThis().name);
auto channel_id = ws.receiveText;
logInfo("Receive channel '%s'.", channel_id);
auto callee = runWorkerTaskH(&workerFuncPingPong, Task.getThis, channel_id);
do {
logInfo("ping");
callee.send("ping");
} while (receiveOnly!string() =="pong");
logInfo("Client disconnected - worker is done. THREAD=%s", Thread.getThis().name);
}
}
voidhelloWorld(HTTPServerRequest req, HTTPServerResponse res)
{
res.writeBody("Hello");
}
voidmain()
{
logInfo("APP::CASE::01");
auto router = new URLRouter;
router.registerWebInterface(newWebsocketService());
router.get("/hello", &helloWorld);
auto settings = new HTTPServerSettings;
settings.port = 8080;
settings.bindAddresses = ["::1", "127.0.0.1"];
auto listener = listenHTTP(settings, router);
scope (exit)
{
listener.stopListening();
}
runApplication();
}
Worker tasks are processed by a fixed set of threads on a first-come-first-serve basis. By default, the number of threads matches the number of logical CPU cores of the system, but this can be customized at startup using vibe.core.core.setupWorkerThreads.
This is generally the most efficient way to handle such workloads, since it avoids the thread creation and resource overhead for a one-task-to-one-thread mapping. But especially if the workloads have a very different duration, this can indeed have the drawback of a non-optimal distribution across threads - and you also need to call vibe.core.core.yield periodically to ensure quasi-parallel processing of tasks within the same thread.
But if you really need one thread per task, instead of runWorkerTask, you can simply create a new Thread and perform the work directly, without any task involved. vibe.core.channel.Channel!T together with a free-list could be used to reuse threads and avoid the creation overhead.
hi all,
question about
runWorkerTaskH
, since I have a CPU intensive task and need to execute it in thread.In the example bellow, for each WS connection a new worker task is started. Since the task will perform heavy computations, I expect that each connection will have its own new thread.
But logs and task manager show me a strange behavior, where computations for different WS are done on same thread???
Maybe I do something wrong?
and here is console output
Above I would expect 4 different thread names since I have 4 ws connections running, but instead I have 4 connections and only 2 threads!
Thanks in advance for help and hints!
The text was updated successfully, but these errors were encountered: