multithreading - User interrupt in Ruby infinite loop (multiple classes)? -
i found question similar mine solution worked me when wrote in 1 simple script. wrote second simple example sort of simulating i'm trying do, , seemed still work.
my simulation was:
class def looper(&block) thread.new loop exit if gets.chomp == 'q' end end loop block.call end end end class b < def looper super puts 'howddyyyy b' end end end
this works fine, exiting when press q<enter>
. however, when tried implement actual project, fails work. i'll post code method in question in child class, parent class literally same example above.
def looper super if obj = object.first(:process_status => status_unprocessed) puts "[object ##{obj.id}] processing..." puts "-" * 60 obj.set_failed if @obj.process(obj) obj.set_processed end puts "-" * 60 puts "[object ##{obj.id}] finished!" puts puts else sleep 10 end end end
so, reason, doesn't work. put puts
new thread (listening q
), , seems output puts before every loop of block.call
. maybe isn't able key, mean, maybe timeframe in have enter q<enter>
way small? i'm not sure, why i'm asking advice here. other guess has methods called within method (process, or possible sequel calls database) blocking other thread(s)?
i'm new threading, have no clue.
okay, everybody. feel little stupid typing up, came solution not 5 minutes later (and 1 had overlooked here on stack overflow).
for facing similar issue in future, ended doing (in parent class):
def looper(&block) interrupted = false trap("int") { interrupted = true } until interrupted block.call end exit end
this manages achieve trying do.
thanks reading!
Comments
Post a Comment