为什么在 Ruby Event Machine 中使用线程?
既然事件机据说是一个基于事件的模型异步I/O库(如node.js),它是单线程的,并使用事件循环来处理并发请求,那么在Ruby应用程序上是否真的有必要关心和使用线程层代码(即处理请求时的rails控制器)?
我更习惯 Node.js 模型,您实际上只需将代码包装在回调中,然后一切都会为您处理好。 (对 kqueue、epoll 等产生新线程的 select() 系统调用是在较低级别的 C++ 实现中处理的),而且 ECMAscript 本质上没有线程。
最近我在尝试学习Event Machine时看到了这段ruby代码:
Thread = Thread.current
Thread.new{
EM.run{ thread.wakeup }
}
# pause until reactor starts
Thread.stop
我只是好奇在ruby环境中基于事件的编程范例中何时使用线程以及什么具体情况需要我们使用它们。
我知道 ruby 在语言中内置了线程(MRI 绿色线程、JRuby JVM 线程),所以使用线程可能很诱人?然而,从我的角度来看,如果您实际上不应该在更高级别的应用程序代码中担心它们,那么它就违背了整个目的,因为基于事件的模型几乎是为了解决这个问题而引入的。
谢谢。感谢任何答案/澄清。
Since event machine is said to be an event based model async I/O library (like node.js) that is single-threaded and uses event loop to handle concurrent requests, is it really necessary to care about and use threading on the ruby application layer code (i.e rails controller when handling requests)?
I'm more used to node.js model where you actually just wrap your code inside the callback, and then everything is taken care of for you. (the select() system call to kqueue, epoll, etc that spawns new threads are handled in the lower level C++ implementation), and also, ECMAscript by its nature doesnt have threads anyway.
Recently I saw this piece of ruby code when trying to learn about Event Machine:
Thread = Thread.current
Thread.new{
EM.run{ thread.wakeup }
}
# pause until reactor starts
Thread.stop
I'm just curious when threads are to be used in the event-based programming paradigm in ruby environment and what specific situation would require us to use them.
I know that ruby has threads built into the language (MRI green threads, JRuby JVM threads) so it may be tempting to use threads? However from my point of view, it kinds of defeats the whole purpose if you're actually not supposed to worry about them in the higher level application code since event based model pretty much is introduced to solve this problem.
Thanks. appreciate any answers/clarifications.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在使用 EventMachine 时,您不能执行 CPU 密集型任务,因为您花在任务上的时间会从反应器中“夺走”,当我知道任务将要发生时,我会使用线程:
在这些情况下,在单独的线程中生成任务允许它完成其工作,而不会阻止反应器完成自己的工作。
另一种选择是使用纤维,这是另一种不同的野兽。
While using EventMachine, you cannot have a cpu intensive task because the time you spend on your task is "taken away" from the reactor, I use threads when I know a task is going to:
In these cases spawning the task in a separate thread allows it to do its job without preventing the reactor from doing its own work.
Another choice is to use fibers which is yet another different beast.
据我所知,线程和状态机之间最大的区别在于,线程将利用多核处理器进行真正的并行处理,而状态机则以串行方式处理所有内容。另一方面,状态机更容易维护数据完整性,因为您不必太担心竞争条件。
The biggest difference between a thread and a state machine, as far as I'm aware, is that threads will take advantage of a multi-core processor to do true parallel processing, while a state machine processes everything in serial. The state machine, on the other hand, is easier to maintain data integrity with since you don't have to worry so much about race conditions.