Performing background work
Some tasks are better performed in the background than in a request-response cycle. Depending on the type of work, you can use a fiber-based task or thread.
Running a fiber-based task
A function or method that returns void
can be started as a fiber-based task with the runTask()
function. The parameters are the delegates (use std.functional.toDelegate()
if you have a function) and possible parameters.
Note
Do not forget to be cooperative in your fiber: call a pseudo-blocking function or yield()
from time to time.
You can use such a background task to collect data about the server. With the next example, you can read the /proc/loadavg
file on Linux and collect the data of the last 10 minutes. On a request from a client, you send some JavaScript code to paint the graph of the values. The code uses the sleep()
function that triggers the switch to the event loop. Collecting the data is very lightweight but the timing is very inaccurate. You should not expect to see a perfect...