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

Observable values not being returned until the process finishes #473

Open
jjv360 opened this issue Jun 18, 2023 · 3 comments
Open

Observable values not being returned until the process finishes #473

jjv360 opened this issue Jun 18, 2023 · 3 comments

Comments

@jjv360
Copy link

jjv360 commented Jun 18, 2023

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:

// 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))
})()
// 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?

@balbatross
Copy link

I think the while loop could in theory be blocking the execution, what happens if you run it without the blocking loop?

@linonetwo
Copy link

You can try add a setTimeout 0 to the blocking code to move it to next event loop

@jjv360
Copy link
Author

jjv360 commented Jul 17, 2023

Hi, this is no longer an issue for me, it happens on Windows WSL2, but doesn't happen on Windows directly or on my Mac, not sure why...

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

No branches or pull requests

3 participants