Tag: eventmachine

Rubyracer(Ruby的V8绑定)执行速度非常慢

因此,我在eventmachine中有一个TCP服务器,而therubyracer用作预先挂起服务器操作(如filter或扩展)的方法。 当服务器没有接收到大量数据时,这一切都很有吸引力,但是当它被淹没时(有时需要它)它变得非常慢。 所以,我做了一个小基准来看看rubyracer与Ruby相比有多慢,当我看到结果时我感到震惊 : user system total real V8: 0.060000 0.000000 0.060000 ( 0.059903) Ruby: 0.000000 0.000000 0.000000 ( 0.000524) 我不介意它是否很慢,说实话,但我不希望它锁定我的整个服务器,直到它完成处理数据。 使用EM::defer并不是一个真正的选择(我试过它,但它有时会产生大量的线程,具体取决于洪水的密集程度)。 我无法绕过洪水,因为我没有设计协议,客户端要求它们就像那样(尽管很可怕)。 基准代码: require ‘v8’ require ‘benchmark’ class User def initialize @name = “smack” @sex = “female” @age = rand(100) @health = rand(100) @level = rand(100) @colour = rand(14) end attr_accessor :name, :sex, :age, […]

如何在EventMachine实现中捕获exception?

我对这个post有类似的问题,我尝试了给定的解决方案,但无济于事。 我的项目是一个Ruby bot,它使用Blather库连接到Jabber服务器。 问题是,当服务器出现问题并且Blather生成exception时,整个程序退出,我没有机会捕获exception。 这是一些显示问题的简单代码。 在localhost上没有运行Jabber服务器,因此Blather客户端抛出exception。 我的印象是EM.error_handler {}能够拦截它,但我从未看到**** ERROR消息,程序就停止了。 🙁 #!/usr/bin/env ruby require ‘rubygems’ require ‘blather/client/client’ EM.run do EM.error_handler { puts ” **** ERROR ” } Blather::Stream::Client.start( Class.new { }.new, ‘echo@127.0.0.1’, ‘echo’) end 我认为问题是Blather也使用EventMachine并且可能正在调用EM.stop,这会导致外部EM实例停止。

如何使用em_mysql2解决’连接仍在等待结果’错误

我在Goliath(eventmachine)下使用em_mysql2的activerecord。 我的用户模型发生了最奇怪的事情。 当我第一次对/ users进行POST时,它只能按预期找到。 当我做第二次POST时,我收到错误。 Mysql2::Error: This connection is still waiting for a result, try again once you have the result: INSERT INTO `users` (… and so on …) 对于我的任何其他模型或路线,都不会发生这种情况。 我假设如果数据库连接处于混乱状态,我会在其他请求上看到相同的错误但是没有 – 所有其他数据库更新和GET请求似乎都可以正常工作。 有谁知道这只会发生在我的用户模型和仅用于User.save操作的情况下? 活动记录是否以某种方式存储它用于执行Model.save的数据库连接并重新使用它? 编辑: 当我写这个问题时我不知道提到我使用ActiveRecord作为ORM。 我也没有提到我异步向Mongo数据库发送请求以获取用户身份validation信息。 我的解决方案 事实certificate,这个错误发生的唯一时间是来自Mongo的响应在MySQL的响应之前回来,这导致MySQL响应被不同的光纤拾取而不是发出请求的光纤。 由于我使用的MySQL2光纤实现使用光纤的objectID来管理连接,这似乎导致了这个问题。 ActiveRecord + MySql2 + Fibers + Goliath中的整体连接池不是完全支持的配置。 (从那时起可能会有一些进展)

最好使用EM.next_tick或EM.defer与Eventmachine进行长时间运行计算?

我试图弄清楚如何在我必须自己实现的长时间运行的计算中使用延迟。 对于我的例子,我想计算前200000个斐波纳契数,但只返回一个。 我对延期的第一次尝试看起来像这样: class FibA include EM::Deferrable def calc m, n fibs = [0,1] i = 0 do_work = proc{ puts “Deferred Thread: #{Thread.current}” if i < m fibs.push(fibs[-1] + fibs[-2]) i += 1 EM.next_tick &do_work else self.succeed fibs[n] end } EM.next_tick &do_work end end EM.run do puts "Main Thread: #{Thread.current}" puts "#{Time.now.to_i}\n" EM.add_periodic_timer(1) do […]

简单使用EM :: Synchrony#sync导致’根光纤’FiberError – 我的错?

这个计划 require ’em-synchrony’ ## v1.0.0 require ’em-hiredis’ ## v0.1.0 module EventMachine module Hiredis class Client def self.connect(host = ‘localhost’, port = 6379) conn = new(host, port) EM::Synchrony.sync conn.connect conn end alias :old_method_missing :method_missing def method_missing(sym, *args) EM::Synchrony.sync old_method_missing(sym, *args) end end end end EventMachine.synchrony do redis = EM::Hiredis.connect redis.set(‘foo’, ‘bar’) puts redis.get(‘foo’) EM.stop end 像这样死 […]

使用Thin和Sinatra异步迭代请求的响应

如果你在Sinatra中的响应返回一个’eachable’对象,那么Sinatra的事件循环将“每个”你的结果并以流式方式产生结果作为HTTP响应。 但是,如果有对Sinatra的并发请求,它将在处理另一个请求之前迭代一个响应的所有元素。 如果我们对某些数据库查询的结果有一个游标,那意味着我们必须等待所有数据在处理并发查询之前可用。 我看过async-sinatra的gem和http://macournoyer.com/blog/2009/06/04/pusher-and-async-with-thin/ ,认为这些可以解决我的问题,但我试过了出这个例子: require ‘sinatra/async’ class AsyncTest < Sinatra::Base register Sinatra::Async aget '/' do body "hello async" end aget '/delay/:n' do |n| EM.add_timer(n.to_i) { body { "delayed for #{n} seconds" } } end end 并且/delay/5请求并不像我期望的那样同时工作,即我同时发出3个请求,Chrome的调试器将响应时间记录为大约5,10和15秒。 我错过了一些设置还是有另一种方法告诉Sinatra / Thin以并发方式处理请求? 更新:这是另一个扳手(或可能清除):运行curl -i http://localhost:3000/delay/5同时具有正确的行为(每个请求在5秒内返回2个)。 运行ab -c 10 -n 50 http://locahost:3000/delay/5 (Apache基准实用程序)也会在总时间(~25秒)内返回合理的值。 Firefox表现出与Chrome相同的行为。 什么是与命令行实用程序不同的浏览器?

Ruby IMAP IDLE并发 – 如何解决?

我正在尝试构建一个(私有的,现在的)Web应用程序,该应用程序将利用IMAP IDLE连接来显示人们到达时的电子邮件。 我很难弄清楚如何将它们组合在一起 – 以及它如何与我的Heroku RoR服务器配合使用。 我已经编写了一个用于连接到IMAP服务器和空闲的基本脚本,看起来像这样(简化): imap = Net::IMAP.new server, port, usessl imap.login username, password imap.select “INBOX” imap.add_response_handler do |response| if resp.kind_of(Net::IMAP::UntaggedResponse) && resp.name == “EXISTS” # New mail recieved. Ping back and process. end end imap.idle loop do sleep 10*60 imap.renew_idle end 这将与IMAP服务器建立一个连接并开始空闲。 如你所见,这是循环阻塞。 我希望我的用户可以同时空闲多个IMAP连接。 最初,我只想将它们中的每一个放在一个线程中,如下所示: Thread.new do start_imap_idling(server, port, usessl, username, password) […]

使用postgresql gem async

我正在使用Goliath (由eventmachine驱动)和postgres gem pg ,目前我正以阻塞的方式使用pg gem: conn.exec(‘SELECT * FROM products’) (例如)和我想知道是否有更好的方式连接到postgres数据库?

为什么Sinatra请求采用EM线程?

Sinatra应用程序接收长时间运行任务的请求,EM.defer它们,在EM的20个线程的内部池中启动它们。 当EM.defer运行超过20个时,EM.defer将它们存储在EM的线程序列中。 但是,似乎Sinatra不会为任何请求提供服务,直到有一个EM线程可以处理它们。 我的问题是,是不是Sinatra假设使用主线程的反应器来服务所有请求? 当我发出新请求时,为什么我会在线程上看到添加? 重现步骤: Access /track/ Launch 30 /sleep/ reqs to fill the threadqueue Access /ping/ and notice the add in the threadqueue as well as the delay 重现它的代码: require ‘sinatra’ #monkeypatch EM so we can access threadpools module EventMachine def self.queuedDefers @threadqueue==nil ? 0: @threadqueue.size end def self.availThreads @threadqueue==nil ? 0: @threadqueue.num_waiting […]

在Windows 7 x86上使用Ruby安装OpenSSL for eventmachine

在Windows 7 x86上使用Ruby安装OpenSSL for eventmachine。 我想在Windows 7 x86 SP1上安装eventmachine gem。 我从http://rubyinstaller.org/downloads/安装了rubyinstaller-2.1.5.exe 我还安装了DevKit-mingw64-32-4.7.2-20130224-1151-sfx.exe 。 我运行命令: gem install eventmachine –platform=ruby 至于现在这是使用eventmachine-1.0.4 。 我得到了以下控制台输出: Temporarily enhancing PATH to include DevKit… Building native extensions. This could take a while… ERROR: Error installing eventmachine: ERROR: Failed to build gem native extension. C:/Ruby21/bin/ruby.exe extconf.rb checking for main() in -lssl… no checking […]