我正在尝试在我的项目中引入一些并发性。
我现在正在使用并行 gem,并且在并行进程中创建对象时遇到问题。
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
允许程序按预期运行。