Limit Workers to max. avail. CPUs#12
Conversation
| this.image = null; | ||
|
|
||
| if (!workerPoolCreated) { | ||
| createWorkerPool(); |
There was a problem hiding this comment.
One liner ifs can also be made an awesome one
!workerPoolCreated && createWorkerPool();
There was a problem hiding this comment.
I forgot a line, that's why I couldn't do the shortcut :)
lib/ImageWorker.js
Outdated
There was a problem hiding this comment.
Thank you, I'm new to flow!
| } | ||
| constructor(props: ImageWorkerProps) { | ||
| super(props); | ||
| this.worker.onmessage = (event: Object) => { |
There was a problem hiding this comment.
What was the purpose of removing the listener from the constructor and moving it to CDM?
There was a problem hiding this comment.
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.
|
Each IMageWorker component will now spawn 4 worker threads. As we've removed the |
|
can you run |
| const blobURL = URL.createObjectURL( | ||
| new Blob([webWorkerScript], { type: 'application/javascript' }) | ||
| ); | ||
| for (let i = 0; i < window.navigator.hardwareConcurrency || 4; ++i) { |
There was a problem hiding this comment.
This will go into an infinite loop
There was a problem hiding this comment.
when you say
i < window.navigator.hardwareConcurrency || 4The 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( |
There was a problem hiding this comment.
why remove this, where is the worker actually being spawned?
There was a problem hiding this comment.
moved to line 42
lib/ImageWorker.js
Outdated
There was a problem hiding this comment.
response is not defined here. When this fn is executed it will break you can change it to
response => response.blob()|
@nitish24p I fixed the mistakes, sorry for those, I was in a hurry. |
lib/ImageWorker.js
Outdated
There was a problem hiding this comment.
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
lib/ImageWorker.js
Outdated
There was a problem hiding this comment.
this should be workerObj.i as per your code
|
There's a big flaw in my current implementation: Edit: I've overlooked, that Workers have the |
Fixes #10