node-haystack Episode 2: Asynchronous and Threading
来源:互联网 发布:阿里云tv应用市场 编辑:程序博客网 时间:2024/04/29 09:37
Asynchronous
To get the full power of machines, async operation seems the only choice. The only problem is which to choose in so many threading models. Simple answer is: libuv, to better and happily work with nodejs.
The official document of libuv is here.
Also you can g-o-o-g-l-e any articles about libuv if you are lucky enough.
Threading
The essential of threading is performing asynchronous operations. Since libuv already provides the work queue for us. So we should use it for good: post our requests into queue, processing request when our turns come and clean the mass we made.
Batching writing or On-spot writing
One important problem is how to performing the file writing. If we have a lot of small-size files and concerning on the writing performance, a better way is combining data of all requests and write to disk once, lately. The pro is obvious: better writing performance or disk IO. The con is obviously too: the request has to wait for the writing to complete, extra information(mostly, the source/sender who made the request and is waiting for result) must be transferred with data so that the processor knows whom to notify when job done. That makes things more complicated. Another consequence of batching writing is the requests are scatted but all complete events are fired at the same time. Further more, writing queue consumes more memory.
On-spot writing, or immediate writing means launching the writing operation immediately once the request arrived. This way works better for large files but worse for small files. Its algorithm is simple and easy to code.
As usual, the Batching writing, as the harder way, must be chosen. To make better batching, we should check the length of waiting queue when pushing request. Once the queue is long enough, we launch the batching writing. To make sure the data will be writ to disk finally if there are no so many requests in queue, a timer is necessary to do the job periodically.
Needed async components
The libuv provides C style interface. It is definitely necessary that have them the C++ cover. There are three components:
- AsyncFile
- AsyncWorker
- AsyncTimer
AsyncFile
Wrapper of libuv file, perform the asynchronous file access.
AsyncWorker
Wrapper of libuv work queue, perform the asynchronous operations.
AsyncTimer
Wrapper of libuv timer, perform periodical operations.
- node-haystack Episode 2: Asynchronous and Threading
- node-haystack Episode 1: What is it and why
- node-haystack Episode 6: Data Structure And Constants
- node-haystack Episode 8: Simple Recovery And Verification
- node-haystack Episode 5: Volume
- node-haystack Episode-4: Wrapper of libuv
- node-haystack Episode 7: Asynchronously manipulate blocks
- node-haystack Episode 9: Manipulate Volume
- node-haystack Episode 10: Node.js add-on
- node-haystack Episode 11: node object of Volume
- node-haystack Episode 3: Callback model in C++
- node-haystack Episode 12: problem of C++ closure
- node-haystack Episode - 12 : A Better Random Generator
- about synchronous and asynchronous.
- ASYNCHRONOUS JAVASCRIPT AND XML
- Asynchronous JavaScript and XML (AJAX) with Java 2 Enterprise Edition
- Hibernate and Multi-threading
- Threading and Queues -- Tensorflow
- linux top load 详解
- C++ 递归
- Java设计模式——模板方法模式
- Gallery----自定义画廊效果
- eclipse新建maven项目和聚合项目
- node-haystack Episode 2: Asynchronous and Threading
- 接上篇《iOS 的Push Notification的证书处理和客户端接收处理》
- 在Centos7下源代码安装配置Nginx
- 线程安全的概念,实现线程安全的几种方法
- 打磨APP(一)
- 大数据,数据分析,机器学习,架构等相关系统名称名词解释
- Android中几种特殊的动画设置方式
- app选项卡
- Java构造和解析Json数据的两种方法详解二:使用JSON-lib库