RejectedSoftware Forums

Sign up

Newbie example for proper multithreading

Hi, trying to get into D and vibe.d programming.

I would like to most efficiently utilize all cores on a server with a vibe.d application.

What is the recommended approach?

a) run as many processes (each on different port) as there are cores and throw in a load balancer (HAProxy?) in front of them?

b) run a single process and use some sort of multi-threading support?

For (b), the main vibe.d page mentions that it fully supports multi-threading, but does not really give an example of how to code it properly.

Simply calling spawn() on every HTTP call seems intuitively the wrong thing to do.
Is taskPool the recommended approach instead?

Thanks
Jacek

P.S. I do have computationally intensive tasks happening in every request, so I know my event loop thread will block if I just code without multi-core support.

Re: Newbie example for proper multithreading

On Wed, 20 Nov 2013 22:26:27 GMT, Jacek Furmankiewicz wrote:

For (b), the main vibe.d page mentions that it fully supports multi-threading, but does not really give an example of how to code it properly.

    auto options = new HTTPServerSettings();
    options |= HTTPServerOption.distribute;
    enableWorkerThreads();

This should spawn thread count equal to processor core count so that they share same listening socket and get assigned incoming connections by OS mechanisms. It can greatly simplify architecture of your application if you need to shared some synchronized state between workers (which is usually to be avoided but reality differs) but for completely independent request handling logic should not given any real advantages vs "multiple processes + load balancer" approach.

That said, worker thread feature has not been tested that much and was mostly added to get rid of my whining :) So if you don't feel the courage to report possible bugs, spawning processes should be safer way to go.

Any sort of threading other than worker model is often harmful to network server performance so there is no point of seeking it "just because you can".

Re: Newbie example for proper multithreading

On Wed, 20 Nov 2013 22:26:27 GMT, Jacek Furmankiewicz wrote:

P.S. I do have computationally intensive tasks happening in every request, so I know my event loop thread will block if I just code without multi-core support.

You may consider:

a) integrating computational code with fiber-even model to yield regularly and let program process stockpiled events. This will act exactly as if multi-threaded but more efficiently.

b) change the design as doing heavy computation while client is waiting sucks :) It can just timeout the connection tired of waiting. Changing client-server communication into non-blocking ones is probably a better idea, be it polls or websockets. Exact usage of threads in depends on the chosen design here. It is probably not that trivial thing to pull off if you don't have much networking programming experience.

Re: Newbie example for proper multithreading

    auto options = new HTTPServerSettings();
    options |= HTTPServerOption.distribute;
    enableWorkerThreads();

Hi, sorry for my lacking D knowledge, but when I do this code I get....

source/app.d(13): Error: 'options |= distribute' is not a scalar, it is a vibe.http.server.HTTPServerSettings
source/app.d(13): Error: incompatible types for ((options) |= (distribute)): 'vibe.http.server.HTTPServerSettings' and 'HTTPServerOption'
Error: DMD compile run failed with exit code 1




Re: Newbie example for proper multithreading

Am 21.11.2013 18:15, schrieb Jacek Furmankiewicz:

    auto options = new HTTPServerSettings();
    options |= HTTPServerOption.distribute;
    enableWorkerThreads();

Hi, sorry for my lacking D knowledge, but when I do this code I get....

source/app.d(13): Error: 'options |= distribute' is not a scalar, it is a vibe.http.server.HTTPServerSettings
source/app.d(13): Error: incompatible types for ((options) |= (distribute)): 'vibe.http.server.HTTPServerSettings' and 'HTTPServerOption'
Error: DMD compile run failed with exit code 1

There is a typo in that snipped. Corrected:

auto settings = new HTTPServerSettings;
settings.options |= HTTPServerOption.distribute;
enableWorkerThreads();
listenHTTP(settings, ...);

Re: Newbie example for proper multithreading

enableWorkerThreads seems to be deprecated. How do I configure multiple worker threads. With default settings, my server is only saturating one of the CPUs.

On Thu, 21 Nov 2013 18:23:02 +0100, Sönke Ludwig wrote:

Am 21.11.2013 18:15, schrieb Jacek Furmankiewicz:

    auto options = new HTTPServerSettings();
    options |= HTTPServerOption.distribute;
    enableWorkerThreads();

Hi, sorry for my lacking D knowledge, but when I do this code I get....

source/app.d(13): Error: 'options |= distribute' is not a scalar, it is a vibe.http.server.HTTPServerSettings
source/app.d(13): Error: incompatible types for ((options) |= (distribute)): 'vibe.http.server.HTTPServerSettings' and 'HTTPServerOption'
Error: DMD compile run failed with exit code 1

There is a typo in that snipped. Corrected:

auto settings = new HTTPServerSettings;
settings.options |= HTTPServerOption.distribute;
enableWorkerThreads();
listenHTTP(settings, ...);



Re: Newbie example for proper multithreading

On Wed, 27 Aug 2014 04:50:20 GMT, Rushi Desai wrote:

enableWorkerThreads seems to be deprecated. How do I configure multiple worker threads. With default settings, my server is only saturating one of the CPUs.

It is enabled by default now, just enabling HTTPServerOption.distribute is enough. However there seem to be an outstanding bug in current master that makes it unusable : https://github.com/rejectedsoftware/vibe.d/issues/777