Open yi-jiayu opened 5 years ago
Would this work for you?
while client.alive?
sleep 1
end
sleep
will put the current running thread to sleep and schedule the execution of another thread.
But I agree that we should think about implementing the built-in way of doing this. Joining UpdateManager thread is a good idea, but I will think a bit more about the public API.
Cool, that works.
Did a simple comparison between the three methods:
Modified Client
:
$ time timeout 5 bundle exec ruby example.rb
real 0m5.020s
user 0m0.496s
sys 0m0.211s
While loop without sleep
:
$ time timeout 5 bundle exec ruby example.rb
real 0m5.017s
user 0m4.720s
sys 0m0.182s
While loop with sleep 1
:
$ time timeout 5 bundle exec ruby example.rb
real 0m5.025s
user 0m0.454s
sys 0m0.175s
The while loop without sleep
spends almost all of its time in user mode, while both joining the UpdateManager thread and a while loop with sleep 1
perform similarly.
Is there a way to register handlers and then loop forever while processing them?
I tried this naive way of doing so:
However this pegs the CPU at 100%.
Here's something I've tried instead:
This assigns the thread returned by
UpdateManager#run
to an instance variable so that it can bejoin
ed in theClient#wait
method.Then I create some signal handlers to call
Client#dispose
on interrupt (which causes the thread started byUpdateManager
to return), and wait on the client:This processes updates continuously without spinning.