In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
It is believed that many inexperienced people don't know what to do about how to use close to achieve event cycle in nodejs. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Close is the last phase of each event cycle of nodejs. Let's see how to use it. We know that for a handle, his use is usually init,start,stop. But what if we have something to deal with after stop a handle? At this point, you can use the close phase. The close phase can be used to close a handle and perform a callback. For example, the memory used to release dynamic requests. The tasks of the close phase are generated by uv_close.
Void uv_close (uv_handle_t* handle, uv_close_cb close_cb) {
/ / closing, but post operations such as callback have not been performed yet
Handle- > flags | = UV_HANDLE_CLOSING
Handle- > close_cb = close_cb
Switch (handle- > type) {
Case UV_PREPARE:
Uv__prepare_close ((uv_prepare_t*) handle)
Break
Case UV_CHECK:
Uv__check_close ((uv_check_t*) handle)
Break
...
Default:
Assert (0)
}
Uv__make_close_pending (handle)
}
Uv_close sets the callback and status, and then adjusts the corresponding close function according to the handle type, which is usually the handle of stop. For example, the close function of prepare.
Void uv__prepare_close (uv_prepare_t* handle) {
Uv_prepare_stop (handle)
}
Then execute uv__make_close_pending to append nodes to the close queue.
/ / plug into the closing queue, which is executed in the closing phase
Void uv__make_close_pending (uv_handle_t* handle) {
Handle- > next_closing = handle- > loop- > closing_handles
Handle- > loop- > closing_handles = handle
}
The resulting nodes are saved in the closing_handles queue and then processed one by one at the close node.
/ / execute the callback of closing phase
Static void uv__run_closing_handles (uv_loop_t* loop) {
Uv_handle_t* p
Uv_handle_t* q
P = loop- > closing_handles
Loop- > closing_handles = NULL
While (p) {
Q = p-> next_closing
Uv__finish_close (p)
P = Q
}
}
/ / execute callback of closing phase
Static void uv__finish_close (uv_handle_t* handle) {
Handle- > flags | = UV_HANDLE_CLOSED
...
Uv__handle_unref (handle)
QUEUE_REMOVE (& handle- > handle_queue)
If (handle- > close_cb) {
Handle- > close_cb (handle)
}
}
Execute callbacks one by one. Close differs from stop in that stop a handle, which is not removed from the event loop, but close a handle, which is removed from the handle queue of the event loop.
Let's look at an example that uses uv_close (omitting some of the code).
Int uv_fs_poll_start (uv_fs_poll_t* handle
Uv_fs_poll_cb cb
Const char* path
Unsigned int interval) {
Struct poll_ctx* ctx
/ / assign a heap memory context structure and a string corresponding to path
Ctx = uv__calloc (1, sizeof (* ctx) + len)
/ / Mount the context to handle
Handle- > poll_ctx = ctx
}
Uv_fs_poll_start is a function that listens for changes to a file. He mounts a heap-based structure in handle. When the monitoring ends, he needs to release this piece of memory.
/ / stop poll
Int uv_fs_poll_stop (uv_fs_poll_t* handle) {
Struct poll_ctx* ctx
Ctx = handle- > poll_ctx
Handle- > poll_ctx = NULL
Uv_close ((uv_handle_t*) & ctx- > timer_handle, timer_close_cb)
}
Uv_fs_poll_stop closes handle through the uv_close function, and the callback passed is timer_close_cb.
/ / release the memory of the context structure
Static void timer_close_cb (uv_handle_t* handle) {
Uv__free (container_of (handle, struct poll_ctx, timer_handle))
}
So in the close phase, it will be whether this piece of memory.
After reading the above, have you mastered how to use close to implement event loops in nodejs? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 300
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.