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

最后都变了- 提交于 2019-12-08 11:17:45

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/2100701/why-cant-i-access-some-library-classes-when-im-in-a-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!