question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Considering support run async func in worker thread?

See original GitHub issue

It seems that ThreadWorker can only accept sync function as args at present. What if provide an options.async, when it sets to be true, then exec async function.

example:

'use strict'
const { ThreadWorker } = require('poolifier')

async function yourFunction (data) {
  return new Promise((resolve, reject) => {
    resolve(1)
  })
}

module.exports = new ThreadWorker(yourFunction, { async: true, maxInactiveTime: 60000 })

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
pioardicommented, May 21, 2020

Version 1.1.0 is released , I will close this issue . Let me know if everything is ok and thanks to contribute on this project !!!

1reaction
NeoyeElfcommented, May 21, 2020

In my use case, I use vm2 to run user’s custom code(like google app scripts). And the vm2 is not isolated, so I need to run in worker threads. And the script may contains async func.

Here is an example:

const { NodeVM } = require('vm2');
const { ThreadWorker } = require('poolifier')

async function execVM(data) {
  const vm = new NodeVM({
    require: {
      context: 'sandbox',
      external: false,
    },
    sandbox: {...someModules},
  });
  const exec = vm.run(`
    async function someAsyncFunc(args) {
      await requestSomeApi()
      doSomeCpuSensitive()
    }
  
    //... some other functions

    module.exports = {someAsyncFunc}
  `);
  return exec[data.func](data.args)
}

module.exports = new ThreadWorker(execVM, { maxInactiveTime: 60000, async: true })
Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple worker threads vs One worker with async/await
The work is mostly IO bound. async lambda with trackedTasks works. I have var t = Task.Run(async lambda); trackedTasks.Add(t); t.ContinueWith( ...
Read more >
Working with threads — AnyIO 3.6.2 documentation
Such operations would normally block the asynchronous event loop, leading to performance issues. The solution is to run such code in worker threads....
Read more >
Task asynchronous programming model - Microsoft Learn
The method runs on the current synchronization context and uses time on the thread only when the method is active. You can use...
Read more >
w3reality/async-thread-worker - GitHub
async -thread-worker presents an abstraction of Web Worker thread communication based on the client-server model. Supported features are:.
Read more >
Introducing asynchronous JavaScript - Learn web development
Here, makeGreeting() is a synchronous function because the caller has to wait for the function to finish its work and return a value...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found