开发者

Why can't I access some library classes when I'm in a thread?

开发者 https://www.devze.com 2022-12-18 01:15 出处:网络
Why does the following require \"bio\" threads = (1..2).map do Thread.new do seqs = [\"gattaca\"] * 5 alignment = Bio::Alignment.new(seqs)

Why does the following

require "bio"

threads = (1..2).map do
  Thread.new do
    seqs = ["gattaca"] * 5
    alignment = Bio::Alignment.new(seqs)
  end
end

threads.each {|th| th.join} ; nil

give this error message?

NameError: uninitialized constant Bio::Alignment
    from (irb):6
    from (irb):10:in `join'
    from (irb):10
    from (irb):10:开发者_高级运维in `each'
    from (irb):10


The bioruby library (or at least some versions of it) use autoload. Autoload isn't thread-safe (at least in ruby 1.8), so if two threads are accessing Bio::Alignment at the same time, you can have errors.

0

精彩评论

暂无评论...
验证码 换一张
取 消