aarshpatel73.medium.com/node-js-worker-threads-for-multithreading-typescript-1e5b88fa76b5
1 Users
0 Comments
14 Highlights
1 Notes
Tags
Top Highlights
JavaScript is known as a single thread programming language.
That means it will execute only one set of instruction at any time in same process.
nothing that required the complexity of multithreading.
Node runs on a single thread, and there is just one process happening at a time in the event loop.
One code, one execution, (the code is not executed in parallel).
This is very useful because it simplifies how you use JavaScript without worrying about concurrency issues.
There is a downside single threading
that code can block the event loop and prevent other requests of being handled.
I have created one worker file which will create one worker thread to execute particular code on separate thread.
So we have to create one js file which read code from ts file and execute it as JavaScript code and that’s why we need ts-node module.
This worker file is create one worker which will execute on another thread.
We have to create both files as worker_threads only excepts js or jsx files
Add your code in migration.ts file which you want to execute on different thread.
Now you have to just call runWorker method from worker.ts file which will execute your code in migration.ts file.
Glasp is a social web highlighter that people can highlight and organize quotes and thoughts from the web, and access other like-minded people’s learning.