{"id":274,"date":"2020-05-17T09:37:17","date_gmt":"2020-05-17T01:37:17","guid":{"rendered":"https:\/\/www.funnymuddy.com\/?p=274"},"modified":"2020-05-17T20:53:26","modified_gmt":"2020-05-17T12:53:26","slug":"libuv-event-loop","status":"publish","type":"post","link":"https:\/\/www.funnymuddy.com\/?p=274","title":{"rendered":"libuv event-loop"},"content":{"rendered":"\n<p>\u6458\u81ea\u6587\u7ae0 <a href=\"http:\/\/docs.libuv.org\/en\/v1.x\/guide\/basics.html\">Basics of libuv<\/a><\/p>\n\n\n\n<p>\u5173\u4e8elibuv\u7684\u6587\u7ae0\u6709\u5f88\u591a\uff0c\u4f46\u4e2a\u4eba\u8ba4\u4e3a\u8fd9\u7bc7libuv\u7684\u5b98\u65b9\u6587\u6863\u8bb2\u7684\u6700\u6e05\u6670\u660e\u4e86\u3002\u5173\u952e\u5b57\uff1a\u4e8b\u4ef6\u9a71\u52a8\uff0c\u5f02\u6b65\uff0c\u975e\u963b\u585e\uff0c\u7ebf\u7a0b\u6c60\u3002\u8fd9\u4e9b\u5173\u952e\u8bcd\u662f\u76f8\u4e92\u5173\u8054\u548c\u4f9d\u8d56\u7684\u3002<\/p>\n\n\n\n<p>Event loop\u53ef\u4ee5\u7ed1\u5b9a\u5230\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u4e5f\u53ef\u4ee5\u540c\u65f6\u8fd0\u884c\u591a\u4e2aevent loop\uff0c\u6bcf\u4e2aevent loop\u7ed1\u5b9a\u5230\u4e00\u4e2a\u4e0d\u540c\u7684\u7ebf\u7a0b\u3002network I\/O\u7684\u5904\u7406\u603b\u662f\u8fd0\u884c\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b(\u53c2\u8003Linux epoll)\u3002libuv\u7ef4\u62a4\u7684\u7ebf\u7a0b\u6c60\u53ea\u662f\u63d0\u4f9b\u7ed9\u6bcf\u4e2afile I\/O\u3002<\/p>\n\n\n\n<p>livuv\/event loop\u597d\u6bd4\u4e00\u4e2a\u9ed1\u76d2\uff0c\u7a0b\u5e8f\u7684\u4e00\u7cfb\u5217event\u5728\u8fd9\u91cc\u6ce8\u518c\uff0c\u901a\u8fc7\u8c03\u7528OS\u7cfb\u7edf\u8d44\u6e90\u5904\u7406\u5b8c\u6210\uff08\u5305\u62eccallback)\u540e\u8fd4\u56de\u5230\u4e3b\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<p>libuv enforces an&nbsp;<strong>asynchronous<\/strong>,&nbsp;<strong>event-driven<\/strong>&nbsp;style of programming. Its core job is to provide an event loop and callback based notifications of I\/O and other activities. libuv offers core utilities like timers, non-blocking networking support, asynchronous file system access, child processes and more.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Event loops<\/h2>\n\n\n\n<p>In event-driven programming, an application expresses interest in certain events and respond to them when they occur. The responsibility of gathering events from the operating system or monitoring other sources of events is handled by libuv, and the user can register callbacks to be invoked when an event occurs. The event-loop usually keeps running&nbsp;<em>forever<\/em>. In pseudocode:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>while<\/strong> there are still events to process:\n    e = get the next event\n    <strong>if<\/strong> there <strong>is<\/strong> a callback associated <strong>with<\/strong> e:\n        call the callback\n<\/pre>\n\n\n\n<p>Some examples of events are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>File is ready for writing<\/li><li>A socket has data ready to be read<\/li><li>A timer has timed out<\/li><\/ul>\n\n\n\n<p>This event loop is encapsulated by&nbsp;<code>uv_run()<\/code>&nbsp;\u2013 the end-all function when using libuv.<\/p>\n\n\n\n<p>The most common activity of systems programs is to deal with input and output, rather than a lot of number-crunching. The problem with using conventional input\/output functions (<code>read<\/code>,&nbsp;<code>fprintf<\/code>, etc.) is that they are&nbsp;<strong>blocking<\/strong>. The actual write to a hard disk or reading from a network, takes a disproportionately long time compared to the speed of the processor. The functions don\u2019t return until the task is done, so that your program is doing nothing. For programs which require high performance this is a major roadblock as other activities and other I\/O operations are kept waiting.<\/p>\n\n\n\n<p>One of the standard solutions is to use threads. Each blocking I\/O operation is started in a separate thread (or in a thread pool). When the blocking function gets invoked in the thread, the processor can schedule another thread to run, which actually needs the CPU.<\/p>\n\n\n\n<p>The approach followed by libuv uses another style, which is the&nbsp;<strong>asynchronous, non-blocking<\/strong>&nbsp;style. Most modern operating systems provide event notification subsystems. For example, a normal&nbsp;<code>read<\/code>&nbsp;call on a socket would block until the sender actually sent something. Instead, the application can request the operating system to watch the socket and put an event notification in the queue. The application can inspect the events at its convenience (perhaps doing some number crunching before to use the processor to the maximum) and grab the data. It is&nbsp;<strong>asynchronous<\/strong>&nbsp;because the application expressed interest at one point, then used the data at another point (in time and space). It is&nbsp;<strong>non-blocking<\/strong>&nbsp;because the application process was free to do other tasks. This fits in well with libuv\u2019s event-loop approach, since the operating system events can be treated as just another libuv event. The non-blocking ensures that other events can continue to be handled as fast as they come in&nbsp;<a href=\"http:\/\/docs.libuv.org\/en\/v1.x\/guide\/basics.html#id2\">[1]<\/a>.<\/p>\n\n\n\n<p>Note<\/p>\n\n\n\n<p>How the I\/O is run in the background is not of our concern, but due to the way our computer hardware works, with the thread as the basic unit of the processor, libuv and OSes will usually run background\/worker threads and\/or polling to perform tasks in a non-blocking manner.<\/p>\n\n\n\n<p>Bert Belder, one of the libuv core developers has a small video explaining the architecture of libuv and its background. If you have no prior experience with either libuv or libev, it is a quick, useful watch.<\/p>\n\n\n\n<p>libuv\u2019s event loop is explained in more detail in the&nbsp;<a href=\"http:\/\/docs.libuv.org\/en\/v1.x\/design.html#the-i-o-loop\">documentation<\/a>.<\/p>\n<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_274\" class=\"pvc_stats all  \" data-element-id=\"274\" style=\"\"><i class=\"pvc-stats-icon small\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img decoding=\"async\" src=\"https:\/\/www.funnymuddy.com\/wp-content\/plugins\/page-views-count\/ajax-loader.gif\" border=0 \/><\/p><div class=\"pvc_clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\u6458\u81ea\u6587\u7ae0 Basics of libuv \u5173\u4e8elibuv\u7684\u6587\u7ae0\u6709\u5f88\u591a\uff0c\u4f46\u4e2a\u4eba\u8ba4\u4e3a\u8fd9\u7bc7libuv\u7684\u5b98\u65b9\u6587\u6863\u8bb2\u7684\u6700&hellip;&nbsp;<a href=\"https:\/\/www.funnymuddy.com\/?p=274\" class=\"\" rel=\"bookmark\">\u9605\u8bfb\u66f4\u591a &raquo;<span class=\"screen-reader-text\">libuv event-loop<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_274\" class=\"pvc_stats all  \" data-element-id=\"274\" style=\"\"><i class=\"pvc-stats-icon small\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img decoding=\"async\" src=\"https:\/\/www.funnymuddy.com\/wp-content\/plugins\/page-views-count\/ajax-loader.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,12],"tags":[25],"class_list":["post-274","post","type-post","status-publish","format-standard","hentry","category-os","category-webapp","tag-nodejs"],"_links":{"self":[{"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=\/wp\/v2\/posts\/274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=274"}],"version-history":[{"count":3,"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=\/wp\/v2\/posts\/274\/revisions"}],"predecessor-version":[{"id":279,"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=\/wp\/v2\/posts\/274\/revisions\/279"}],"wp:attachment":[{"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}