Похожие презентации:
EventLoop in NodeJS
1. EventLoop in NodeJS
Sergey Avanesyan2.
S E N I O R S O F T WA R E E N G I N E E R• More than 5 years production experience in web development.
• More than 1 year experience in EPAM Systems.
• Experience in pure JavaScript.
• Focus on writing SPA using React and Redux.
• Interest in Algorithms and Computer Science.
2
3. GOALS
UNDERSTAND HOW NODEJS WORKS UNDER A HOODALLOWS BETTER UNDERSTAND CODE
EFFECTIVELY WRITE BACK-END JAVASCRIPT HIGH-LOAD APPLICATION
3
4. Nodejs architecture
NODEJS ARCHITECTURE5. NodeJS architecture
6. NodeJS architecture
7. Eventloop
EVENTLOOP8.
The event loop is what allows Node.js to performnon-blocking I/O operations — despite the fact that
JavaScript is single-threaded — by offloading
operations to the system kernel whenever possible.
8
9. EventLoop structure
910. EventLoop structure and source code
1011. Example
12. Initialization stage
>12
13. Initialization stage
> START13
14. Initialization stage
> START14
15. Initialization stage
> START15
16. Initialization stage
> START16
17. Initialization stage
> START17
18. Initialization stage
> START18
19. Initialization stage
> START19
20. Initialization stage END
> STARTEND
20
21. First circle START: nextTickQueue
> STARTEND
21
22. First circle: Other microtasks queue
> STARTEND
Next Tick
22
23. First circle
> STARTEND
Next Tick
Promise
23
24. First circle: nextTickQueue
> STARTEND
Next Tick
Promise
24
25. First circle: Phase timers
> STARTEND
Next Tick
Promise
Promise Next Tick
25
26. First circle: nextTickQueue
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
26
27. First circle: Other microtasks queue
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
27
28. First circle: Phase pending callbacks
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
28
29. First circle: nextTickQueue
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
29
30. First circle: Other microtasks queue
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
30
31. First circle: phase idle, prepare
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
31
32. First circle: nextTickQueue
3233. First circle: Other microtasks queue
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
33
34. First circle: phase poll
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
34
35. First circle: nextTickQueue
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
35
36. First circle: Other microtasks queue
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
36
37. First circle: check
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
37
38. First circle: nextTickQueue
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
38
39. First circle: other microtasks queue
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
39
40. First circle: phase close callbacks
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
40
41. First circle END
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
41
42. Second circle START: phase timers
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
42
43. Second circle: phase pending callbacks
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
43
44. Second circle: idle, prepare
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
44
45. Second circle: phase poll
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
45
46. Second circle
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
46
47. Second circle: nextTickQueue
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
47
48. Second circle: other microtasks queue
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
48
49. Second circle: phase check
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
49
50. Second circle: phase close callbacks
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
readFile setImmediate
50
51. Second circle END
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
readFile setImmediate
51
52. Third circle START: phase timers
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
readFile setImmediate
52
53. Third circle START: phase pending callbacks
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
readFile setImmediate
readFile setTimeout
53
54. Third circle START: phase idle, prepare
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
readFile setImmediate
readFile setTimeout
54
55. Third circle START: phase poll
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
readFile setImmediate
readFile setTimeout
55
56. Third circle START: phase check
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
readFile setImmediate
readFile setTimeout
56
57. Third circle START: phase close callbacks
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
readFile setImmediate
readFile setTimeout
57
58. Third circle END
> STARTEND
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
readFile setImmediate
readFile setTimeout
58
59. Check loop alive condition
5960. Check loop alive condition
6061. Check loop alive condition: NO
6162. EventLoop complete work
6263. threadpool
THREADPOOL64.
Threadpool can be used to run user code and getnotified in the loop thread.
Its default size is 4, but it can be changed at startup
time by setting the UV_THREADPOOL_SIZE
(process.env.UV_THREADPOOL_SIZE) environment
variable to any value (the absolute maximum is
128).
64
65. EventLoop and ThreadPool
6566. Example
6667. Example
6768. EventLoop and ThreadPool
6869. ThreadPool
6970. ThreadPool
7071. ThreadPool
7172. ThreadPool
7273. ThreadPool and EventLoop
7374. Example
7475. Example
7576. materials
M AT E R I A L Shttps://epa.ms/eventloop