- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
这是我的测试用例,我发现EM并不比一般的TCP服务器快
EM 服务器:
require 'rubygems'
require 'benchmark'
require 'eventmachine'
class Handler < EventMachine::Connection
def receive_data(data)
operation = proc do
# simulate a long running request
a = []
n = 5000
for i in 1..n
a << rand(n)
a.sort!
end
end
# Callback block to execute once the request is fulfilled
callback = proc do |res|
send_data "send_response\n"
end
puts data
EM.defer(operation, callback)
end
end
EventMachine::run {
EventMachine.epoll
EventMachine::start_server("0.0.0.0", 8080, Handler)
puts "Listening..."
}
和我的基准测试:
require 'rubygems'
require 'benchmark'
require 'socket'
Benchmark.bm do |x|
x.report("times:") do
for i in 1..20
TCPSocket.open "127.0.0.1", 8080 do |s|
s.send "#{i}th sending\n", 0
if line = s.gets
puts line
end
puts "#{i}th sending"
end
end
end
end
最佳答案
与线程相比简单,而不是速度。在此处查看更多见解:EventMachine: Fast and Scalable Event-Driven I/O Framework
适用于您的问题的引文:
A lot has been written about the fact that event-driven programs are not theoretically any faster than threaded ones, and that is true. But in practice, I think the event-driven model is easier to work with, if you want to get to extremely high scalability and performance while still ensuring maximum robustness. I write programs that have to run for months or years without crashing, leaking memory, or exhibiting any kind of lumpy performance, so in practice, event-driven programming works better. Now, here's the problem with event-driven programming: you have to write "backwards." A threaded model stores your program state (inefficiently) in local variables on a runtime stack. In EM you have to do that yourself, which is very unintuitive to programmers who are used to threads. This is why I'm interested in fibers, because it opens the possibility of writing what looks to the programmer like blocking I/O, but still is evented and uses no threads.
关于ruby - EventMachine的优势是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5830147/
我的控制台上出现以下错误 root@comp09:~# gem install eventmachine Building native extensions. This could take a
我在 JRuby 中安装了 EventMachine(我使用的是 Win32)。当我尝试服务器示例时,出现错误 require 'eventmachine' 错误是: C:\dev\em>jruby
我有一些 Deferrables,当它们全部完成时我需要在回调中做一些事情。 defers[0].callback do defers[1].callback do defer
我正在编写一个应用程序,它使用 EventMachine 来中继来自服务的命令。我想重新使用与服务的连接(而不是为每个新请求重新创建它)。该服务从模块方法启动,并且该模块提供给 EventMachin
更新: 我找到了简单的答案,这完全是我自己的错:我将上面的代码存储在一个名为“eventmachine.rb”的文件中。因此,当我需要“eventmachine”时,它只是导入了相同的文件,其中不包含
好的,我有代码在后台使用 Cramp\Tramp => EventMachine。代码: class RetrieveController 0.12' gem 'tramp', '~> 0.2' g
每个人总是明确提到 next_tick 将在主线程中执行。但是定时器和回调/错误反馈呢?它们是否也保证在主线程中运行? 最佳答案 无论 Ruby 版本如何,EM 在 react 器线程内运行除 EM.
在 ruby 1.9.2-p290 上编译 eventmachine 0.12.10 时出现以下错误输出: g++ -shared -o rubyeventmachine.so binder.o cm
我收到以下错误: eventmachine.rb:534:in `start_tcp_server': no acceptor (port is in use or requires root pri
我有一个使用 eventmachine 编写的小型 HTTP 服务器脚本,它需要调用外部脚本/命令并通过反引号 (``) 调用。当提供不运行反引号代码的请求时,一切都很好,但是,一旦我的 EM 代码执
我写了一个像这样的简单的 EventMachine 服务器: EventMachine.run do EventMachine::WebSocket.start(:host => HOST, :p
“run” block 是否在 EM 中作为一个整体执行(没有上下文切换)?在此示例中,if 子句中是否存在竞争条件? EventMachine.run { @current_value = 0
获取端口已在使用错误。 require 'em-websocket' EM.run { EM::WebSocket.run(:host => "192.168.1.100", :port =>
我对一般编程还很陌生,我在客户端和服务器端都使用 EventMachine 来打开它们之间的 websocket 连接。 我的问题出在客户端,以及由于网络连接问题导致连接丢失时。 def websoc
如何终止 EventMachine 中正在运行的进程?下面是一个示例,我正在启动 10 个进程,然后我试图将它们全部删除(但它不起作用)。我的目标是不要有“完成”输出。 require "rubyge
我使用 EventMachine LineText2 协议(protocol),我想在每次按下键盘上的字符时触发 receive_line 方法,而不仅仅是在输入新行时触发。有没有办法改变这种默认行为
这是我的代码: EventMachine.run { conn = EM::Protocols::HttpClient2.connect request.host, 80 req =
我正在研究使用 EventMachine 支持的 twitter-stream rubygem 来跟踪和捕获推文。我对整个事件编程有点陌生。我如何判断我在事件循环中所做的任何处理是否导致我落后?有
我的第一个问题是关于 Ruby 的。我正在尝试测试 Reactor 循环内的 EventMachine 交互 - 我想它可以归类为“功能”测试。 假设我有两个类 - 服务器和客户端。我想测试双方 -
我正在使用合适的 Redis EM gem(在我的例子中为“em-hiredis”)读取 EventMachine react 器循环中的 Redis 集,并且必须检查某些 Redis 集是否包含级联
我是一名优秀的程序员,十分优秀!