Support for running parallel operations on a fiber?
TazmanianD opened this issue · 1 comments
@bjouhier I was wondering if you'd considered adding some support for running a set of operations in parallel on the same fiber. In the context of threads, this wouldn't make any sense as a thread can only do one thing at a time but from my understanding of fibers, they don't necessarily have that same restriction.
So if I'm running on a fiber and I want to execute, say, 3 different operations in parallel and have them run on the same fiber (perhaps so I can access some state on the fiber), how might you recommend doing that?
I could execute the operations in parallel using a Promise.all like this:
function thingsInParallel() {
return wait(Promise.all([
run(() => thing1()),
run(() => thing2()),
]));
}
But that creates new fibers for each one (which is probably not ideal for performance either). Is there any way something like this might work:
function thingsInParallel() {
return waitParallel([
parallel(() => thing1()),
parallel(() => thing2()),
]);
}
@TazmanianD The easy way is to create a separate fiber for each operation. Two ways to write it:
// with Promise.all
function thingsInParallel() {
return wait(Promise.all([run(thing1), run(thing2)]));
}
// with run and wait only
function thingsInParallel() {
const [p1, p2] = [run(thing1), run(thing2)];
return [wait(p1), wait(p2)];
}
Another solution is to write the code that runs in parallel with regular callbacks and promises (no fibers) and to wait
on the global operation.
But you cannot run two functions written with f-promise in parallel on the same fiber. This is because a fiber always resumes where it yielded. So if it yielded inside thing1
, it will resume inside thing1
and there is no way to move thing2
forwards during that time.
Fibers are inexpensive (compared to threads) and context switching between fibers is fast. So it should not be a problem to run a reasonable number of operations in parallel on separate fibers. Let's say a few hundreds.