Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting error 'another command is already in progress' for async requests #13

Closed
quezacoatl opened this issue Mar 14, 2014 · 2 comments
Closed
Labels

Comments

@quezacoatl
Copy link

I frequently get 'another command is already in progress' when performing asynchrous requests simultaneously. Am I doing something wrong?

I do something like this:

EM.run do
    df1 = pg.query_defer('select * from foo')
    df2 = pg.query_defer('select * from foo')
    df1.callback { |result|
      puts Array(result).inspect
    }
    df1.errback {|ex|
      raise ex
    }
    df2.callback { |result|
      puts Array(result).inspect
    }
    df2.errback {|ex|
      raise ex
    }
  end
/home/rubyuser/.rvm/gems/ruby-2.1.1/gems/eventmachine-1.0.3/lib/em/deferrable.rb:158:in `call'
/home/rubyuser/.rvm/gems/ruby-2.1.1/gems/eventmachine-1.0.3/lib/em/deferrable.rb:158:in `set_deferred_status'
/home/rubyuser/.rvm/gems/ruby-2.1.1/gems/eventmachine-1.0.3/lib/em/deferrable.rb:198:in `fail'
/home/rubyuser/.rvm/gems/ruby-2.1.1/gems/em-pg-client-0.3.2/lib/pg/em.rb:491:in `async_autoreconnect!'
/home/rubyuser/.rvm/gems/ruby-2.1.1/gems/em-pg-client-0.3.2/lib/pg/em.rb:568:in `block in exec_prepared_defer'
@royaltm
Copy link
Owner

royaltm commented Mar 14, 2014

You can't run concurrent parallel operations on the single PG::EM::Client connection instance. Neither you couldn't do it on a single PG::Connection.

What you need is a connection pool, which fortunately is included in this gem:

require 'pg/em/connection_pool'

pg = PG::EM::ConnectionPool.new(size: 10, dbname: 'foo')

where size: is a maximum number of allowed connections per pool, and the other options are passed directly to connection instances.

Then your example should work.

See "Connection Pool" chapter in README.md for some more examples.

@quezacoatl
Copy link
Author

Ok, I see. Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants