为什么我在线程中不能访问某些库类? [英] Why can't I access some library classes when I'm in a thread?

查看:74
本文介绍了为什么我在线程中不能访问某些库类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要这么做

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

给出此错误消息?

NameError: uninitialized constant Bio::Alignment
    from (irb):6
    from (irb):10:in `join'
    from (irb):10
    from (irb):10:in `each'
    from (irb):10

推荐答案

bioruby库(或至少它的某些版本)使用自动加载.自动加载不是线程安全的(至少在ruby 1.8中是安全的),因此,如果两个线程同时访问Bio :: Alignment,则可能会出错.

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.

这篇关于为什么我在线程中不能访问某些库类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆