Code Monkey home page Code Monkey logo

node-worker's Introduction

Node Worker

This repository hosts a sample of using workers in Node. consumer.js is a worker script which listens for messages from coming from index.js which uses producer.js to generate data to feed to the worker.

If the worker receives new messages at a faster rate than it is able to process the existing messages, it will cache them in memory and process them at the next available opportunity.

The protocol between the main and worker scripts is such that the main script keeps sending messages to process and the worker keeps processing them until null is sent instead of a message. At that moment, the worker knows no more messages will come, it finishes up any cached messages and sends null back to the main script to signal it is done processing.

To see the caching behavior in action, play around with the delay values in the producer and consumer scripts. Right now, it is set up such that the producer is twice as fast as the consumer, resulting in a nice pyramid/sawtool style cache pattern.

To-Do

Prototype using this for SVG Screencast to off-load patching and optimizing

SVG Screencast currently uses a single thread for both capturing the frames to feed into the algorithm and executing the processing itself, which if there are multiple regions to process within a single screenshot, causes stutter, because the optimization step can be quite heavy, hoarding the thread for itself for a long time and starving the capturing process of resources to continue running uninterrupted.

At the moment, I am using setImmediate in some critical areas to split the heavy processing up into more reasonable chunks, but that only eases the stutter instead of eliminating it. Moving the patch and optimize phases into their own worker would ensure smooth operation of the capture phase on the main thread of the application.

There might be issue with the type of data that can be sent to a worker, namely, it is very likely that instead of passing the crop function as a part of the screenshot, it will have to be done using messaging so it will no longer be a part of a screenshot, instead, it will be another parameter to SVG screencast.

In the initial proof of concept, this crop implementation using messaging can be allowed to marshall the buffer it wants to crop back to the main thread so we don't have to keep another cache of possibly-croppable buffers and maintain that until we know the buffer has been processed and will not be asked for cropping anymore, but in the final implementation, it would be ideal to communicate to the caller the frame SVG Screencast is on and let it know when to release cached buffers once that frame's number has passed.

node-worker's People

Contributors

tomashubelbauer avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.