andywer / andywer/threads.js

Observable values not being returned until the process finishes

未关闭
#473 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
TypeScript
星标
3.5k
派生
173
PR 合并指标
30 天内没有已合并 PR

描述

Hi, I'm trying to train a neural network with brain.js in a thread (using Node v20, I tried v18 as well), and I can't seem to get updates from the Observable until the entire thread has finished it's work... I've reproduced the issue in the simplest form here:

```js
// main.js
import { spawn, Worker, Thread } from 'threads'

(async function() {
let worker = await spawn(new Worker("./worker.js"))
worker.longRunningCode().subscribe(progress => console.log(progress))
})()
```

```js
// worker.js
import { expose } from "threads/worker"
import { Observable } from "observable-fns"

expose({
longRunningCode() {
return new Observable(observer => {
observer.next("Started")
for (let i = 0 ; i < 5 ; i++) {
observer.next("Progress " + i)
let startedAt = Date.now()
while (Date.now() - startedAt < 1000) { /* long running code */ }
}
observer.next("Completed")
observer.complete()
})
}
})
```

I would expect to see Progress 0, Progress 1 etc appearing 1 second apart in the console log. Instead, nothing happens for 5 seconds and then I get all the logs at once at the end...

I'm not sure if I'm doing something wrong, or if this is a bug?

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。