using-worker-threads-with-konduktiva

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
using-worker-threads-with-konduktiva [2024/01/04 05:30] steve.wangusing-worker-threads-with-konduktiva [2024/03/20 18:36] (current) steve.wang
Line 3: Line 3:
 **Overview: ** Sometimes running too many actions in the same nodejs thread can cause Konduktiva sequencing to be delayed because of the heavy load on the system and the single threaded nature of nodejs. To avoid this issue, we can create other threads and run heavy actions there. Reading the [[https://nodejs.org/api/worker_threads.html#worker-threads|documentation about nodejs workers]] for about 5 minutes or enough to understand the basic function of nodejs workers is recommended. **Overview: ** Sometimes running too many actions in the same nodejs thread can cause Konduktiva sequencing to be delayed because of the heavy load on the system and the single threaded nature of nodejs. To avoid this issue, we can create other threads and run heavy actions there. Reading the [[https://nodejs.org/api/worker_threads.html#worker-threads|documentation about nodejs workers]] for about 5 minutes or enough to understand the basic function of nodejs workers is recommended.
  
-**Things To Note:** This tutorial assumes you have already installed Konduktiva successfully using the [[https://github.com/renickbell/konduktiva|Konduktiva installation]] instructions and have read through and understood the [[:first_steps|first steps tutorial]]. The tutorial also assumes that you have imported Konduktiva to the //K// variable and assigned any of the example MusicalEnvironments to the //e// variable. +**Things To Note:** This tutorial assumes you have already installed Konduktiva successfully using the [[https://github.com/renickbell/konduktiva|Konduktiva installation]] instructions and have read through and understood the [[:first_steps|first steps tutorial]]. The tutorial also assumes 2 things. One, Konduktiva has been assigned to the //K// variable. Two, user created a Musical Environment using the //setUpMusicalEnvironment// function using //K.defaultConfigurationObject// as the first argument and //'exampleMidiPlayer'// as the third argument then, assigned the output to the //e// variable. 
 +<code javascript> 
 +const K = require('konduktiva'
 +let e = K.setUpMusicalEnvironment(K.defaultConfigurationObject,4,'exampleMidiPlayer', K.exampleMusicalEnvironmentsExtraConfig) 
 +</code>
 ===== giveWorkerWork ===== ===== giveWorkerWork =====
  
 To create a worker and make it run code use the function //giveWorkerWork//. The actions you want the worker thread to run should be in form of a string. That string should include all other functions and dependencies the worker thread might need to perform those actions. To create a worker and make it run code use the function //giveWorkerWork//. The actions you want the worker thread to run should be in form of a string. That string should include all other functions and dependencies the worker thread might need to perform those actions.
  
-Functions have been created to allow the user to easily create and use worker threads in nodejs. These functions have been included with Konduktiva (main thread on github for now). TO explain this imagine this situation +Functions have been created to allow the user to easily create and use worker threads in nodejs. These functions have been included with Konduktiva (main thread on github for now). To explain how the function works, this imagine this situation. 
-For example, I want the worker thread to use this function called simpleAddition and I want the worker thread to call this function passing the number //4// as an argument and return the result to the main thread. I can do this.+I want the worker thread to use this function called simpleAddition and I want the worker thread to call this function passing the number //4// as an argument and return the result to the main thread:
 <code javascript> <code javascript>
 function simpleAddition (a){ function simpleAddition (a){
Line 24: Line 27:
 **IMPORTANT**: The information that you want the worker thread to be return must be passed to the //returnToParent// function (only available to the worker thread). After this function is called by the worker thread, the main thread(you original nodejs thread) will be unable to receive more information from that worker thread but the worker thread will be allowed to finish running all the code provided to it before automatically exiting(unless an error has occurred in which case the worker thread will be terminated). **IMPORTANT**: The information that you want the worker thread to be return must be passed to the //returnToParent// function (only available to the worker thread). After this function is called by the worker thread, the main thread(you original nodejs thread) will be unable to receive more information from that worker thread but the worker thread will be allowed to finish running all the code provided to it before automatically exiting(unless an error has occurred in which case the worker thread will be terminated).
  
-The worker thread close itself before all the code runs by using the //[[https://nodejs.org/api/worker_threads.html#event-exit|proccess.exit()]]// command in the code(in string form) provided when the worker is created:+The worker thread can be prompted to close itself before all the code runs by using the //[[https://nodejs.org/api/worker_threads.html#event-exit|proccess.exit()]]// command in the code(in string form) provided when the worker is created:
 <code javascript> <code javascript>
 process.exit() process.exit()
 </code> </code>
  
-//await// is needed because the //giveWorkerWork// command returns a promise. A promise is returned because it needs to wait for the worker thread to complete it's task.+//await// is needed because the //giveWorkerWork// function returns a promise. A promise is returned because it needs to wait for the worker thread to complete it's task.
  
 Here is another example which shows the worker thread using require to load a library: Here is another example which shows the worker thread using require to load a library:
Line 36: Line 39:
 let testResult = await K.giveWorkerWork(requireTest) let testResult = await K.giveWorkerWork(requireTest)
 </code> </code>
 +
 +Here is an example of adding the information given by workers to the musical environment. In this example, I will user workers to create a QuantizedMap for filterMap. The contents will be the same as chromatic.
 +<code javascript>
 +let QWorkerCode = `let {QuantizedMap} = require('konduktiva'); let A = require("array-toolkit"); returnToParent(new QuantizedMap(10, A.buildArray(12, x => {return x}), A.buildArray(10, x => {return x})))`
 +let QResults = await K.giveWorkerWork(QWorkerCode)
 +e.modeFilters.sameAsChromatic = new K.QuantizedMap(QResults.keyspan, QResults.keys, QResults.values)
 +</code>
 +
 +
  • using-worker-threads-with-konduktiva.1704375016.txt.gz
  • Last modified: 2024/01/04 05:30
  • by steve.wang