Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does the call stack need to be empty for the later code to be run? #159

Open
nx10 opened this issue Nov 30, 2021 · 2 comments
Open

Does the call stack need to be empty for the later code to be run? #159

nx10 opened this issue Nov 30, 2021 · 2 comments

Comments

@nx10
Copy link

nx10 commented Nov 30, 2021

I use later in my package to call R from a C++ background thread in a safe way.
From looking in the source code I assume later waits for some regular callbacks and, if the call stack is empty the later code is run.
(Please correct me if these assumptions are wrong)

Does the call stack need to be completely empty for technical reasons or could later run code while R is in a loop or executing other code?
For example: I would like to execute code while R waits for keyboard inputs:

later::later(function() { print("Hello from later") }, 1) 
readline("Waiting for keyboard input") # wait for more than 1 sec. then pressing enter

Expected output:

(Text is printed 1 sec after execution, before enter is pressed)

Waiting for keyboard input
[1] "Hello from later"
[1] ""

Actual output:

(Text is printed after enter is pressed)

Waiting for keyboard input
[1] ""
[1] "Hello from later"

Possibly similar issue: #152

@wch
Copy link
Member

wch commented Nov 30, 2021

Callbacks in the event loop can be run in these two ways:

  • In an interactive session, when the call stack is empty and the console is idle.
  • When run_now() is called.

So you can run the event loop at any time by calling run_now().

@nx10
Copy link
Author

nx10 commented Nov 30, 2021

Thank you for the quick response. Could you maybe go into more detail why the call stack needs to be empty? Is this a technical limitation or a design decision by later?

Edit: It seems like Rhttpd.c does not check the call stack. Would it be possible to add a priority flag to later which disables the check?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants