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

Limit Workers to max. avail. CPUs #12

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

kurtextrem
Copy link
Contributor

Fixes #10

this.image = null;

if (!workerPoolCreated) {
createWorkerPool();
Copy link
Collaborator

@manjula-dube manjula-dube Feb 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One liner ifs can also be made an awesome one

!workerPoolCreated && createWorkerPool();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot a line, that's why I couldn't do the shortcut :)

}

/** Marks worker `index` as available. */
function setFree(index: int) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change int => number

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I'm new to flow!


state = {
isLoading: true,
imgSrc: ''
}
constructor(props: ImageWorkerProps) {
super(props);
this.worker.onmessage = (event: Object) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the purpose of removing the listener from the constructor and moving it to CDM?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved everything to the constructor back again. My original idea was that it should only load the image when truly needed, but the constructor is called before CDM.

@nitish24p
Copy link
Owner

Each IMageWorker component will now spawn 4 worker threads. As we've removed the worker.terminate call, will those threads still be active when a componentWillUnMount gets called?

@nitish24p
Copy link
Owner

can you run npm run eslint to see the lint errors and fix those

const blobURL = URL.createObjectURL(
new Blob([webWorkerScript], { type: 'application/javascript' })
);
for (let i = 0; i < window.navigator.hardwareConcurrency || 4; ++i) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will go into an infinite loop

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why so?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when you say

i < window.navigator.hardwareConcurrency || 4

The browser starts evaluating from the left and this will be treated as
(i < window.navigator.hardwareConcurrency) || (4)
even if the value of i goes to say 200, the above will then become
200 < window.navigator.hardwareConcurrency || 4
Which will translate to false || 4 and will always be true

class ImageWorker extends Component<ImageWorkerProps, ImageWorkerState> {
image: HTMLImageElement;
worker = new Worker(URL.createObjectURL(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove this, where is the worker actually being spawned?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to line 42

@@ -3,15 +3,10 @@
import React, { Component } from 'react';

const webWorkerScript = `
const handleResponse = () => response.blob();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

response is not defined here. When this fn is executed it will break you can change it to

response => response.blob()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@kurtextrem
Copy link
Contributor Author

@nitish24p I fixed the mistakes, sorry for those, I was in a hurry.

@@ -34,33 +29,71 @@ const wrappedComponent = WrappedComponent => props => {
return <WrappedComponent {...props} />;
};

/** Have we initiated the worker pool already? */
const workerPoolCreated = false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you will have to change this to a let and reassign it to a true, after the createWorkerPool fn is invoked. Else everytime there will be 4 workers created as workerPoolCreated will always be false

this.loadImage(event.data);
setFree(workerObj.index);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be workerObj.i as per your code

@kurtextrem
Copy link
Contributor Author

kurtextrem commented Feb 14, 2018

There's a big flaw in my current implementation: .onmessage can only exist once it seems. Which means currently ever new instance of ImageWorker replaces the callback function.
This means we need something like a Observer pattern.

Edit: I've overlooked, that Workers have the .addEventListener interface too, so that's not a problem anymore.

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

Successfully merging this pull request may close these issues.

3 participants