-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
Does a postTask
API make sense for Node.js (in any way)?
#33436
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
Comments
I do not think this would be useful for Node.js like it is for browsers. In browsers, the JS vm has a lot of idle time, so it can process those tasks and keep the UI reactive. Note that delaying tasks increase memory usage. In Node.js, we have either a lot of users or a single user that wants a response as soon as possible. In both cases, we should finish our compute as soon as we can.. and postponing tasks for the future increase the overall processing time because of the added gc costs :(. I think Electron can benefit from this |
First, I think it's good for source code compatibility, even if it's mostly no-op it will make easier creating isomorphic code. Do we need that in nodejs for? Using scheduling can give me mush cheaper "threads" in terms of memory copying or manually share stuff using arraybuffers. |
I agree with mcollina, we don't really have anywhere to push low priority tasks to. In your server example, new requests coming in will continually displace your lower priority task and it will never run. The scheduler.yield api seems interesting but in terms of node it would have to be equivalent to |
For reference here is the issue I opened in their repo WICG/scheduling-apis#17 I think a good next step would be for someone to look at whether or not Node libraries implement these sort of schedulers in the same way frontend libraries do or not. |
I suppose one could have a type of libuv task that only gets called if the rest the libuv tasks return nothing from poll, but generally it's very hard to estimate this properly. I suppose in practically doing it "properly" doesn't matter, as long as the user understands that it cannot be estimated exactly and that the task still blocks polling while it is run. (Also, not sure this really has anything to do with "timers".) |
The postTask API has an optional "delay" parameter, effectively making it a timer. This might have further implications if the the delay is tied to a priority and can't be implemented as a setTimeout + postTask at that priority. |
If it can't be implemented that way your delayed task might actually be less efficient overall, lol. |
Here is my concern: we have a history of not collaborating too well with browsers and spec bodies on APIs (fetch, abortcontroller, streams, promises etc) and then being frustrated when APIs users ask for don't make sense for node. If browsers implement this in all likelihood users will start asking for it in a year or two. If we have concrete feedback API wise or quality wise then we should provide it. Otherwise it might play out like fetch where:
For example - we're probably going to have issues with TaskController (like we do with AbortController today). |
Hmm.. might be a good time to point people to Piscina... A worker thread pool module that @addaleax and I have been working on for the past month. We've done a significant amount of analysis on various workloads and performance and will be publishing a blog post soon about it. The module supports a runTask API, implements various concurrency and queue options, and supports a priority queue mode, among several other things. We could likely, fairly easily, build a version of this proposed browser API on top of it but there are countless ways where the processing semantics aren't going to be even nearly the same. |
One strategy for implementing this would be to add a check handle to the libuv queue that, when invoked, is set to run for a target period of time. During that time, it pulls callbacks from a priority queue and executes them. If, after it executes one callback, the target time has elapsed, it returns control to the event loop. If the time hasn't yet elapsed, it picks the next item and invokes it. However, because we can't actually guarantee the order of check handles in the loop (user code could add more), inserting this as a permanent immediate that runs at the start of each loop turn would be necessary... it would essentially become a The performance benefits of this (versus using a worker thread pool) are... questionable... but could be easily explored using |
Thinking about this further, I'm -1 on adding this in core at this time. It could make for a useful userland module and could easily be implemented as such. |
To be fair, I don't actually understand what the API does - if it's just sugar and doesn't map logically to Node I wholeheartedly agree. |
I would hazard a guess it is useful in some way regarding frame updates in the browser GUI, although I don't know for certain. |
I think discussion has stalled so I'll close this and if the project ever decides it's a good idea let's reopen. |
Uh oh!
There was an error while loading. Please reload this page.
Hey, web browsers and framework vendors are working on a
postTask
API for dispatching tasks with particular priorities.It is specified here and here is a basic example:
I opened an issue for Node.js use cases there. Copying it here:
I've been thinking of exploring adopting this API or experimenting with it in Node.js with the use case I've been thinking about is similar main thread contention. I have a server that serves a large number of users. That server needs to manage QoS of various levels of requests. Some requests should be very fast while for other requests I really wouldn't mind the users to wait a few milliseconds.
For example if I have a request that hashes an image and stores it for later retrieval - it might be OK for that request to wait until the CPU is idle. Work that takes more than a few milliseconds typically gets put in a (persistent) queue on servers but there is certain work I probably wouldn't mind waiting for.
The same goes for CLI tools, webpack might be interested in performing certain kinds of analysis in a lower priority than actually outputting the compiled code. TypeScript might "emit" before it's done type-checking to give users a snappier experience etc.
The main issue I see here is that Node.js already exposes threads and processes (with worker_threads and child_process) - so users already have access to the operating system scheduler. That said, it would be cool if some code could be made universal and shared here.
cc @shaseley, @sebmarkbage and @acdlite from the
postTask
side and @addaleax @BridgeAR @nodejs/workers @nodejs/open-standards from the Node side.Is this interesting or relevant at all for Node.js and if so should we eventually strive to adopt it?
The text was updated successfully, but these errors were encountered: