Skip to content

Instantly share code, notes, and snippets.

@jeromeetienne
Last active June 8, 2017 12:30
Show Gist options
  • Save jeromeetienne/2221b0e273f0dd78035c4c0431ff7915 to your computer and use it in GitHub Desktop.
Save jeromeetienne/2221b0e273f0dd78035c4c0431ff7915 to your computer and use it in GitHub Desktop.
Webworkers pool - Minimal standalone implementation
self.addEventListener('message', function(event) {
console.log('in worker - received event', event)
self.postMessage('started')
}, false);
<body><script>
var workerURL = 'worker-pool-worker.js'
// create all workers
var workers = []
workers.push( new Worker(workerURL) )
workers.push( new Worker(workerURL) )
workers.push( new Worker(workerURL) )
workers.push( new Worker(workerURL) )
var nWorkers = workers.length
var worker = workers[1]
worker.postMessage('start')
worker.onmessage = function(event) {
console.log('in mainthread - received event', event)
if( event.data === 'started' ){
console.log('in mainthread - worker started', event)
}
}
</script></body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment