Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fiber scheduler support #459

Open
uvlad7 opened this issue Feb 16, 2025 · 0 comments
Open

Fiber scheduler support #459

uvlad7 opened this issue Feb 16, 2025 · 0 comments

Comments

@uvlad7
Copy link
Contributor

uvlad7 commented Feb 16, 2025

Ruby introduced non-blocking fibers and fiber schedulers lately, is there any way to make Curl::Easy#perform non-blocking in the same way?
As perform releases GIL under the hood, I expected it to work this way already, but it doesn't seem so:

async_crawler.rb
#!/usr/bin/env -S mise exec [email protected] -- ruby

gem 'curb', '1.0.9'
require 'curl'

gem 'awesome_print', '1.9.2'
require 'awesome_print'

gem 'async', '2.23.0'
require 'async'
gem 'fiber_scheduler', '0.13.0'
require 'fiber_scheduler'
gem 'io-event', '1.9.0'
require 'io/event'

curls = Array.new(3) { Curl::Easy.new }

tasks_arr = curls.size.times.map do
  5.times.map do
    {
      url: 'https://httpbin.org/status/200',
    }
  end
end

def crawl(curl, url)
  curl.url = url
  start_time = Time.now.to_f
  curl.perform
  { code: curl.response_code, start_time: start_time, response_time: Time.now.to_f }
end

##### crawl using fiber_scheduler ####

Fiber.set_scheduler(FiberScheduler.new)

curls.zip(tasks_arr).each do |curl, tasks|
  Fiber.schedule do
    tasks.each do |task|
      Fiber.schedule do
        task.merge!(crawl(curl, task[:url]))
      end
    end
  end
end

Fiber.set_scheduler(nil)

#### end of fiber_scheduler usage ####

ap tasks_arr

#### crawl using Async ####

curls.zip(tasks_arr).each do |curl, tasks|
  Async do
    tasks.each do |task|
      Async do
        task.merge!(crawl(curl, task[:url]))
      end
    end
  end
end

#### end of Async usage ####

ap tasks_arr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant