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
FWIW, it's best to only invoke greenlet once per function - generally at definition time. Dynamically creating workers to do single tasks will always have a nontrivial performance overhead.
Just going to clarify for folks finding this issue - Greenlet does re-use the worker thread for all calls, but only if you hoist the greenlet() definition so it's being called one time for the given async function:
// BAD: this creates a new worker every time getPage() is called:exportfunctiongetPage(url){constgetJson=greenlet(asyncurl=>{returnawait(awaitfetch(url)).json();});returngetJson(url);}
// GOOD: this uses the same worker for each call to getPage():constgetJson=greenlet(asyncurl=>{returnawait(awaitfetch(url)).json();});exportfunctiongetPage(url){returngetJson(url);}
hey @developit, I wanted to offload some functions to a worker and I was thinking if this 👇 solution is good enough? (the alternative is using those postMessage thingies by hand, which I hate):
@developit I love workerize(-loader)! But it didn't play well with Next.js, so I ended up using worker-plugin (which is also by you? :o) along with comlink.
So, I guess, if anyone is trying to bundle workers in Next.js, use that combo, it works perfectly. Otherwise, stick to greenlet and/or workerize.
(you should probably mark the last 3 comments as off-topic :) )
I needed to import the AWS SDK, that's why it wasn't an option :(
PS. If anyone needs to bundle web workers in Next.js, use Parcel (microbundle had a babel-related error) to build your workers to the /public folder. Then use comlink.
Activity
developit commentedon May 29, 2018
This could probably be adapted (and simplified) to suit:
https://github.com/proxy/gist.github.com/developit/65a2212731f6b00a8aaa55d70c594f5c
FWIW, it's best to only invoke greenlet once per function - generally at definition time. Dynamically creating workers to do single tasks will always have a nontrivial performance overhead.
developit commentedon Oct 1, 2019
Just going to clarify for folks finding this issue - Greenlet does re-use the worker thread for all calls, but only if you hoist the greenlet() definition so it's being called one time for the given async function:
imedadel commentedon Apr 23, 2020
hey @developit, I wanted to offload some functions to a worker and I was thinking if this 👇 solution is good enough? (the alternative is using those postMessage thingies by hand, which I hate):
developit commentedon Apr 23, 2020
@imedadel that works, yup! There's also workerize, which is basically like Greenlet but with support for "exporting" multiple methods from the worker.
imedadel commentedon Apr 24, 2020
@developit I love workerize(-loader)! But it didn't play well with Next.js, so I ended up using
worker-plugin(which is also by you? :o) along withcomlink.So, I guess, if anyone is trying to bundle workers in Next.js, use that combo, it works perfectly. Otherwise, stick to greenlet and/or workerize.
(you should probably mark the last 3 comments as off-topic :) )
developit commentedon Apr 24, 2020
Oh I didn't mean the loader - there is a non-loader version of workerize that uses the same internals as Greenlet, works at runtime.
imedadel commentedon Apr 24, 2020
I needed to import the AWS SDK, that's why it wasn't an option :(
PS. If anyone needs to bundle web workers in Next.js, use Parcel (microbundle had a babel-related error) to build your workers to the
/publicfolder. Then use comlink.Otherwise use greenlet/workerize.