Wednesday, 15 July 2015

how do i run an asynchronous loop in ruby? -



how do i run an asynchronous loop in ruby? -

i need execute process runs several admin scheme commands. want maintain sudo timestamp current while runs in case process run long.

i have next code, not seem work.

sudo_keep_alive = thread.start def sudo sleep 5.minutes `sudo -v` sudo end sudo end at_exit sudo_keep_alive.kill end

is there convention this?

update

the reason cannot run script has root, there other scheme commands script runs cannot run root. each command needs responsible running it's own admin commands. script can potentially take considerable amount of time run, wanted maintain sudo timestamp fresh in event command needs it.

to reply other question, there improve way run asynchronous loop.

by using head-tail recursion (def sudo; do_something; sudo; end) risk running systemstackerror @ around 10000 calls (see how favorite language handle deep recursion? ).

instead, utilize regular old ruby loop.

thread::new loop sleep 300 # 5.minutes not base of operations ruby, comes activesupport call_my_function end end

as mentioned david unric, there no need kill thread using at_exit, main process automatically kill active threads when finishes.

ruby asynchronous

No comments:

Post a Comment