Pregunta

I'm trying to use EM::Synchrony to speed up my queries by making them async. Following along with the examples from the github page here I'm making 2 asynchronous queries:

EventMachine.synchrony do
    db = EventMachine::Synchrony::ConnectionPool.new(size: 2) do
        Mysql2::EM::Client.new(
                :host => config[:server],
                :username => config[:user],
                :password => config[:pwd],
                :database => config[:db_name]
                )
    end

    multi = EventMachine::Synchrony::Multi.new
    multi.add :a, db.aquery("
        select count(distinct userid) from my_table
        where date = '2013-09-28'
        ")
    multi.add :b, db.aquery("
        select count(distinct userid) from my_table
        where date = '2013-09-27'
        ")
    res = multi.perform
    puts res

    # p "Look ma, no callbacks, and parallel MySQL requests!"
    # p res.responses[:callback][0]

    EventMachine.stop
end

> #<EventMachine::Synchrony::Multi:0x00000001eb8da8>

My question is how do I setup a callback to actually get the values returned by the queries? What I would like to do is once the queries are finished, aggregate them back together and write to another table or csv or whatever. Thanks.

¿Fue útil?

Solución 2

What I found is that the following code will give me the results from the queries:

res.responses[:callback].each do
        |obj|
        obj[1].callback do
            |rows|
            rows.each do
                |row|
                puts row.inspect
            end
        end
    end
$ruby async_mysql.rb
{"count(distinct ui)"=>159}
{"count(distinct ui)"=>168}

Otros consejos

Maybe you don't need Synchrony? They are async anyway. https://github.com/brianmario/mysql2#eventmachine

But if you do need then probably the answer is

    res.responses[:callback][:a]
    res.responses[:callback][:b]

https://github.com/igrigorik/em-synchrony/blob/master/lib/em-synchrony/em-multi.rb#L17

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top