0

プロジェクトに並行性を導入しようとしています。

私は現在、並列ジェムで遊んでいますが、並列プロセス内でオブジェクトを作成する際に問題が発生しています。

Parallel.map(["a"], :in_processes => 8) do |i|
  ActiveRecord::Base.connection_pool.with_connection do
    User.create
  end
end

結果:

/Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/parallel-0.8.1/lib/parallel.rb:279:in `dump': no _dump_data is defined for class Binding (TypeError)
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/parallel-0.8.1/lib/parallel.rb:279:in `process_incoming_jobs'
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/parallel-0.8.1/lib/parallel.rb:257:in `block in worker'
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/parallel-0.8.1/lib/parallel.rb:250:in `fork'
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/parallel-0.8.1/lib/parallel.rb:250:in `worker'
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/parallel-0.8.1/lib/parallel.rb:238:in `block in create_workers'
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/parallel-0.8.1/lib/parallel.rb:237:in `each'
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/parallel-0.8.1/lib/parallel.rb:237:in `create_workers'
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/parallel-0.8.1/lib/parallel.rb:201:in `work_in_processes'
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/parallel-0.8.1/lib/parallel.rb:106:in `map'
    from (irb):15:in `irb_binding'
    from /Users/tyler/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/irb/workspace.rb:80:in `eval'
    from /Users/tyler/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/irb/workspace.rb:80:in `evaluate'
    from /Users/tyler/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/irb/context.rb:254:in `evaluate'
    from /Users/tyler/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/irb.rb:159:in `block (2 levels) in eval_input'
    from /Users/tyler/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/irb.rb:273:in `signal_status'
    from /Users/tyler/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/irb.rb:156:in `block in eval_input'
    from /Users/tyler/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/irb/ruby-lex.rb:243:in `block (2 levels) in each_top_level_statement'
    from /Users/tyler/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `loop'
    from /Users/tyler/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `block in each_top_level_statement'
    from /Users/tyler/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `catch'
    from /Users/tyler/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `each_top_level_statement'
    from /Users/tyler/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/irb.rb:155:in `eval_input'
    from /Users/tyler/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/irb.rb:70:in `block in start'
    from /Users/tyler/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/irb.rb:69:in `catch'
    from /Users/tyler/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/irb.rb:69:in `start'
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
Parallel::DeadWorker: Parallel::DeadWorker
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/parallel-0.8.1/lib/parallel.rb:51:in `rescue in work'
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/parallel-0.8.1/lib/parallel.rb:48:in `work'
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/parallel-0.8.1/lib/parallel.rb:216:in `block (4 levels) in work_in_processes'
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/parallel-0.8.1/lib/parallel.rb:352:in `with_instrumentation'
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/parallel-0.8.1/lib/parallel.rb:215:in `block (3 levels) in work_in_processes'
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/parallel-0.8.1/lib/parallel.rb:210:in `loop'
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/parallel-0.8.1/lib/parallel.rb:210:in `block (2 levels) in work_in_processes'
    from /Users/tyler/.rvm/gems/ruby-1.9.3-p392@arcsite_mysql/gems/parallel-0.8.1/lib/parallel.rb:65:in `block (2 levels) in in_threads'

並列ブロックにバリエーションがあると、同じエラーが発生するようです。への呼び出しを削除するとcreate、プログラムが期待どおりに機能するようになります。

4

2 に答える 2

0

あなたが返しているものはダンプ可能ではないように見えるので、nil を返すか Parallel.each を代わりに使用してください おそらく with_connection は実際にはユーザーオブジェクトを返さず、何か奇妙なものを返します...完全なユーザーの代わりに user.attributes を返そうとすることもできます物体

于 2013-10-09T14:51:07.183 に答える
0

http://ruby.zigzo.com/2012/01/29/the-parallel-gem-and-postgresql-oh-and-rails/

上記の Mario の投稿によると、PostgreSQL では複数のスレッドに同じ接続を使用することは許可されていません。MySQLが同じかどうか疑問に思っています。さらに、MySQLはアクセス中にロックをかける可能性があります。PostgreSQL で並列作業を行うためのソリューションは次のとおりです: ActiveRecord::Base.connection.reconnect!

于 2015-05-23T21:48:38.897 に答える