Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Introduction
Multithreading is a process of executing two or more threads simultaneously. The most programming languages provide an API for convenient work with threads and parallelism. The developer can focus on application logic, not on the communication channel, synchronization or architecture setup.
Node 10.5 added support for worker_threads module with an experimental flag. But starting from Node 11.7 this feature is available out of the box. Itâs a good start for multithreading paradigm in Node.
worker_threads has a huge potential, so support for this module was added to Odi from early stages. As always, Odi goal is to provide convenient, clean and minimalistic API (with magic đ§ââïž), so developers can focus on development, not on setup.
Project Setup
From the last article, Odi got new CLI command which initializes project, defines scripts and installs all required dependencies.
odi init
Currently, there are only a few options:
-j, --jsx-templating add jsx templates-d, --database add typeorm integration
Structure
By default, the project will have the following structure.
All required dependencies will be installed during command execution based on the template type. Also if -j flag was provided, views folder will be added in the project root directory.
Source Files
There are 2 predefined source files in the basic template.
The index.ts file that contains basic Odi configuration. By default, the server port is set to 8080
And the home.controller.ts file with a simple controller definition.
Thatâs enough to start with Odi and development.
Scripts
For now, the only npm start script is available out of the box. In the next release, npm test command will be available using Mocha and Chai.
Simply run the following command
npm start
This script includes compilation and actual running, so after completion, you can easily open the browser and check http://localhost:8080 URL.
Threads
By design, Node is single threaded with non-blocking I/O. Such approach has many pros and cons. The main advantage is simplicity. The developer does not need to care about threads manipulations, data synchronization and etc. But any resource-intensive tasks will block the event loop.
Worker threads can process resource-intensive operations, so the main thread is always available. Itâs really important for Server-Side applications, as any blocking task will delay accepting and processing of new client requests.
Task
Letâs create the resource-intensive (blocking) function for getting an answer (Yes or No) based on random values generation.
Mathematical operations in most cases CPU intensive, so itâs a great example for our goals. Running this function with 200_000_000 factor takes ~5 sec for execution.
Blocking
As mentioned above, any blocking operation wonât allow other tasks to execute until itâs finished.
The best way to understand blocking is UI. Letâs add simple CLI loader to our application using Ora library, just for example.
First of all, we need to install it.
npm install ora @types/ora
And change the Controller method in the following way. When the handler is triggered, the loader will appear in the terminal and will be spinning until our calculations are finished. Also, the time that was used for request processing will be printed.
Letâs start our server and trigger handler from the browser.
The loader is not spinning, as our calculation blocked the process. The loader must have the possibility to re-render frames every 80 milliseconds but canât do it, as the event loop is blocked by getAnswer call.
Consequences
Letâs imagine that we have this code in the real application. The handler will block accepting and processing new clients requests. It will seriously affect the client experience. Such operations must be placed into other application or in the other thread.
Workers
Odi provides convenient API for multithreading. The developer does not need to think about any type of setup.
Definition
Itâs really easy to define Worker in Odi application and container. There are some similarities with Service definition. Letâs wrap getAnswer function.
Only Worker decorator is required for definition. Now we can inject it in the controller as other dependencies.
Note, await keyword must be added before the worker method call, even if itâs not async, as communication between threads is done in an asynchronous way.
Thatâs all! đ§ââïž The method will be executed in another thread and the result will be returned to the main.
Review
Now, example with UI loader can be tested.
Everything is working. The loader is spinning, as the code is running in another thread, so UI can re-render frames.
Check
To be sure that the method was processed in another thread, simply change getAnswer in the next way.
Information about thread will be available right in the console.
Comparison
As you can see above, zero configuration is required for workes setup and processing. No event emitters, event handlers, filename and etc are required like in the official example. Odi cares about initialization, messaging, method calls and errors handling.
Limitations
There are no limitations in addition to basic ones. Remember that the worker is something like another application, so runtime instances canât be accessed between different threads. Also, Dependency Injection container canât be accessed over the threads, so every single thread will have its own container.
Use Cases
Basically, workers threads can be used in next approaches:
- Background and scheduled tasks
- Resource-intensive operations
- Queue-based processing
Those approaches can be easily improved and adapted for each particular need, but all of them leads to performance improvements and application flexibility.
More
You can check Odi repo on GitHub.
Also, there is another interesting article about Odi.
Building chat with Odi (Node.js)
Thanks for reading! Feel free to leave any feedback, ideas or questions.
If you like Odi, simply support us with start on GitHub. đâš
Keep reading, much more interesting things will be shipped in the next updates! đđ§ââïž
Threads without efforts in Odi was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.