diveintonode_examples icon indicating copy to clipboard operation
diveintonode_examples copied to clipboard

事件循环处理的自身代码在哪个线程上执行?

Open lookforit opened this issue 8 years ago • 11 comments

题目中 自身代码 指的是,创建主循环,从观察者中取请求对象等操作。 按我的理解,不可能在IO线程池中,是在js主线程还是node的其他线程? 另外,callback肯定在js主线程上执行,那么时间循环应该不包括callback的执行吧。

请教了。

lookforit avatar Jun 21 '16 07:06 lookforit

主线程。JS代码和事件循环都是在主线程之行。

JacksonTian avatar Jun 21 '16 07:06 JacksonTian

谢谢! 答案有点意外。 比如,如果callback费时,岂不是会影响事件循环,对某些观察者无法响应?

在 2016/6/21 15:06, Jackson Tian 写道:

主线程。JS代码和事件循环都是在主线程之行。

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/JacksonTian/diveintonode_examples/issues/8#issuecomment-227357740, or mute the thread https://github.com/notifications/unsubscribe/ABzgbnJkD34aT283qx1LZsXeVCPxmkjcks5qN416gaJpZM4I6a4q.

lookforit avatar Jun 21 '16 07:06 lookforit

嗯。

关于费时的定义,可以参见: https://gist.github.com/jboner/2841832

JacksonTian avatar Jun 21 '16 07:06 JacksonTian

好的。 另外一个建议,关于数据库异步操作原理,书中提及的不多,能否指点一二?

在 2016/6/21 15:17, Rila 写道:

谢谢! 答案有点意外。 比如,如果callback费时,岂不是会影响事件循环,对某些观察者无法响应?

在 2016/6/21 15:06, Jackson Tian 写道:

主线程。JS代码和事件循环都是在主线程之行。

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/JacksonTian/diveintonode_examples/issues/8#issuecomment-227357740, or mute the thread https://github.com/notifications/unsubscribe/ABzgbnJkD34aT283qx1LZsXeVCPxmkjcks5qN416gaJpZM4I6a4q.

lookforit avatar Jun 21 '16 07:06 lookforit

数据库异步操作本质是网络异步操作,在TCP上根据协议封装操作而已。对调用方来说 没有什么特别的地方。

客户端实现上倒是有些细节比较有意思。

我会在第二版中加入这方面的内容。

JacksonTian avatar Jun 21 '16 09:06 JacksonTian

ok。 客户端数据库异步操作是否需要单独实现数据库的观察者,以便通知事件循环操作 完成? 另外,此时数据库的操作还是在IO线程池中进行吗?

在 2016/6/21 17:01, Jackson Tian 写道:

数据库异步操作本质是网络异步操作,在TCP上根据协议封装操作而已。对调用 方来说 没有什么特别的地方。

客户端实现上倒是有些细节比较有意思。

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/JacksonTian/diveintonode_examples/issues/8#issuecomment-227381799, or mute the thread https://github.com/notifications/unsubscribe/ABzgbkhGrLuBMpt4cHJq9e3FKP0KzRsJks5qN6hpgaJpZM4I6a4q.

lookforit avatar Jun 21 '16 09:06 lookforit

TCP。不用IO线程池。

JacksonTian avatar Jun 21 '16 09:06 JacksonTian

感谢您耐心的回复,打扰到您了。 但我真的有点糊涂,如您所述,网络IO是不需要进入IO线程池的吗?

我注意到《libuv doc》上的一段话: “The libuv filesystem operations are different from socket operations. Socket operations use the non-blocking operations provided by the operating system. Filesystem operations use blocking functions internally, but invoke these functions in a thread pool and notify watchers registered with the event loop when application interaction is required ” 上面指出socket操作确实有别于磁盘io,由于其非阻塞特性。 该如何理解网络IO与主线程的交互?

在 2016/6/21 17:52, Jackson Tian 写道:

TCP。不用IO线程池。

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/JacksonTian/diveintonode_examples/issues/8#issuecomment-227393685, or mute the thread https://github.com/notifications/unsubscribe/ABzgbpUy1K_DIWZ3oBWBnfiFlYTcsnFYks5qN7RzgaJpZM4I6a4q.

lookforit avatar Jun 22 '16 00:06 lookforit

socket操作使用epoll/kqueue,就在主线程上操作。

JacksonTian avatar Jun 22 '16 06:06 JacksonTian

如您所讲,epoll和IO线程池没有直接关系,前者由内核提供,负责多路IO复用, 后者由libuv实现,承载IO具体执行工作。

在 2016/6/22 14:07, Jackson Tian 写道:

socket操作使用epoll/kqueue,就在主线程上操作。

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/JacksonTian/diveintonode_examples/issues/8#issuecomment-227652169, or mute the thread https://github.com/notifications/unsubscribe/ABzgbhB6Zv6MfBw-9s4RyWdf4XoOUP41ks5qONESgaJpZM4I6a4q.

lookforit avatar Jun 22 '16 08:06 lookforit

之前有看过netty的实现思路,感觉跟这里的磁盘IO的处理机制差不多,虽然他处理的是网络IO操作,事件的操作只能在单线程中操作。

happyshrm avatar Oct 21 '17 23:10 happyshrm