Description
Edit: I updated the title of my issue. Now I'm looking for a way to run later and RestRserve together and communicate between the two. Please skip to this comment:
I will leave my previous comments for context.
Hello, I'm still learning about later
and event loops. I would appreciate any help.
I wrote this script to test later
and create a "private event loop". I'm still learning about these. Is there any documentation somewhere with examples of how to use them? All I found was the help pages but with no examples - toy use case demos would be very helpful - I want to use it to get data from an API and run a REST API without blocking the global even loop (the REST API).
I'm more familiar with just using later::later
by itself but not with private event loops.
Anywho, I wrote this script that runs well in interactive mode but only opens and closes with no printing if run with Rscript
. In the end this script I want to write with the loop is meant to be server side code so I have to be able to run it using Rscript
.
I plan on using this in a RestRserve
server, so I need this to run without blocking the global event loop - hence, I started looking into private event loops.
I tried to keep the global loop open by sleeping for 10 seconds; the initial print of "Hello world!" worked, but not my loop.
#!/usr/bin/env Rscript
box::use(later)
print("Hello World!")
# Define the function to be executed
print_hello <- function() {
print(paste("Hello World:", Sys.time()))
later$later(print_hello, delay = 0.1) # Schedule the next execution in 100 ms
}
# Create a private event loop
private_loop <- later$create_loop()
# Run the function in the private event loop
later$with_loop(private_loop, {
later$later(print_hello, delay = 0.1) # Schedule the initial execution in 100 ms
later$run_now() # Run the event loop
})
# pause
Sys.sleep(10)