Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for running parallel operations on a fiber? #7

Open
TazmanianD opened this issue Aug 30, 2017 · 1 comment
Open

Support for running parallel operations on a fiber? #7

TazmanianD opened this issue Aug 30, 2017 · 1 comment
Labels

Comments

@TazmanianD
Copy link

TazmanianD commented Aug 30, 2017

@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()),
  ]);
}
@bjouhier
Copy link
Member

bjouhier commented Sep 29, 2017

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants