andywer / andywer/threads.js

Can I require stuff and initialize services and objects outside the function of a function worker?

オープン
#368 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る
enhancement question
主要言語
TypeScript
スター
3.5k
フォーク
173
PR マージ指標
30日以内にマージされた PR はありません

説明

Hello and thank you for this amazing package. I've being using it for years at this point, and it allowed for really cool things.

I have this long-running project that uses this package, and I recently updated it from version 0.8 to 1.6, doing every adjustment needed to the code. I must say I really like how the new version works, much better and cleaner than v0. I have a question that I can't find the answer to from the code and the documentation though.

Suppose I have a pool of workers, with each worker using its own instance of one or more services, like the API client for a specific library (specifically, the Firebase and APN clients to send push notifications to mobile devices). I want to initialize the service when I spawn every worker in the pool for the first time, but I want the services to be already initialized with the same instance from before when a worker is reused. Of course, whenever a worker dies and a new one spawns, the new one will have to reinitialize them and get new instances. No problem about it.

So we're talking about something close to this.
```node
const Worker = require("threads/worker");
const someOtherLibrary = require('some-other-library');
const someConfiguration = require('./config/some-configuration');

const service1 = someOtherLibrary.initializeService1(someConfiguration);
const service2 = someOtherLibrary.initializeService2(someConfiguration);

Worker.expose(async function() {
service1.doSomething(someConfiguration);
service2.doSomethingElse(someConfiguration);
...
});
```
Would this work? Is it right in your vision for this package? If not, how can I correct it?

Thank you.

EDIT: I already made a few attempts and this seems to work, but I wanted to know your own thoughts about it.

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。