How to start/end reactor? #110
Replies: 1 comment
-
Thanks so much for your feedback.
Yes it is possible but you take on the responsibility of controlling it correctly and this isn't what I'd consider a typical use case. Also you need to manually run the reactor for a short time in your own loop, e.g. require_relative "async"
# browser.on(:start) do
reactor = Async::Reactor.new
# Schedule task:
reactor.async do |task|
while true
task.sleep(1)
puts "Hello from task"
end
end
10.times do
# Host the reactor in your own run loop:
reactor.run do |task|
# Run for a fixed duration:
#task.sleep 1
# Run for one iteration:
task.yield
# Stop the reactor loop:
reactor.interrupt
end
# Other stuff
sleep 0.5
end
#browser.on(:exit) do
reactor.stop I don't know how your run loop works, but essentially you must iterate the event loop every now and then. Calling I would suggest you take a look at https://github.com/socketry/async-rspec/blob/master/lib/async/rspec/reactor.rb too - this shows how to use a per-spec reactor which might make more sense for your use case. |
Beta Was this translation helpful? Give feedback.
-
Hi, thank you for providing very awesome async library for Ruby.
I have an issue on using Async in my browser-automation library.
I understand this async library requires root reactor session with
Async { ... }
orAsync::Reactor.run{ ... }
.Sometimes it is hard to define an async session using block, for example:
Can we define Async reactor session without block?
Beta Was this translation helpful? Give feedback.
All reactions