socketry / async

An awesome asynchronous event-driven reactor for Ruby.
MIT License
2.04k stars 85 forks source link

Sleep Hook #305

Closed tleish closed 4 months ago

tleish commented 4 months ago

In using Falcon, does async include a hook to monkey-patch Kernel#sleep with Async#sleep?

em-synchrony has the following hook/logic you can add to your code if using something like Goliath.

# Redirects to EM::Synchrony.sleep
require "em-synchrony"
EM::Synchrony.on_sleep do |*args|
  EM::Synchrony.sleep(*args)
end
ioquatix commented 4 months ago

On Ruby 3.1+ and Async 2+, Kernel::sleep is redirected to the event loop

CRuby: https://github.com/ruby/ruby/blob/fb4df44d1670e9d25aef6b235a7281199a177edb/process.c#L5162-L5170

Async: https://github.com/socketry/async/blob/2c5f92ed2027e50dbfa9ce599505498843c61687/lib/async/scheduler.rb#L151-L158