async: NameError: uninitialized constant inside Async::Task

I don’t seem to have scope for my ActiveRecord model inside an Async::Task.

For my model named F I get:

NameError: uninitialized constant F

My implementation:

Async do
  # Queue of up to 10 items:
  items = Async::LimitedQueue.new(10)

  # Five producers:
  5.times do
    Async do |task|
      # while true
      while F.unchecked.count.positive?
        # t = rand
        f = F.unchecked.first
        f.do_checking!
        t = check_f(f)

        task.sleep(t)
        items.enqueue(t)
      end
    end
  end

  # A single consumer:
  Async do |task|
    while item = items.dequeue
      puts "dequeue -> #{item}"
    end
  end
end

I also tried using ::F but got the same error.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Thanks for all your help 😃

A boolean actually. I changed it to task.sleep(0.1) and got it working.

IRC, any reference F makes to autoload can also break in the same way

Correct. If the reference happens at class-level, it is going to be resolved in cascade. No problem in this case. However, if the methods being executed trigger autoloads in turn, you’d have the same sync issues. Eager loading is the safest bet if that is the case.