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

future state is "running" after it has been resolved #193

Closed
keithing opened this issue Feb 2, 2018 · 5 comments
Closed

future state is "running" after it has been resolved #193

keithing opened this issue Feb 2, 2018 · 5 comments
Labels

Comments

@keithing
Copy link

keithing commented Feb 2, 2018

On my machine (Ubuntu 14.04) it's possible for a future to be resolved yet have a state of "running". I'm not sure if this is intentional or not, since it seems possible to know whether a future is "finished" or "failed" before value is called, but maybe that's not the future design philosophy.

Here's a reproducible example:

library(future)                                                                 
                                                                                
# Run futures in a different process                                            
plan("multiprocess", workers=2)                                                 
                                                                                
# Create a future to sleep for 4 seconds                                        
fut <- future({                                                                 
  Sys.sleep(4)                                                                  
  2 + 2                                                                         
})                                                                              
                                                                                
# Test that I can do stuff while I'm waiting for future                         
print("Parallel!")                                                              
                                                                                
# Wait for future to be resolved                                                
while (!resolved(fut)) {                                                        
  Sys.sleep(1)                                                                  
}                                                                               
                                                                                
# This should be in a completed state like "finished" or "failed"               
print(fut$state)
@keithing keithing changed the title future state is "running" after is has been resolved future state is "running" after it has been resolved Feb 2, 2018
@keithing
Copy link
Author

keithing commented Feb 2, 2018

FWIW, withplan("sequential"), behavior is different and the final line prints "finished" instead of "running".

@HenrikBengtsson
Copy link
Collaborator

Yes, the state only becomes "finished" when the value has been collected internally, e.g.

[...]
> print(fut$state)
[1] "running"

> value(fut)
[1] 4

> fut$state
[1] "finished"

Are you looking into this to understand what's happening under the hood or are you building something that relies on it? Please note that state and many other fields are "internal" fields not part of the official API, so be careful as they might change. Let me know if you needs something added to the official API that you can otherwise only get from the state field.

@keithing
Copy link
Author

keithing commented Feb 5, 2018

Awesome. I just came across as I was looking into handling errors that happen in a future. I didn't realize it was an internal field and it just surprised me. I'm not relying on it.

Let me know if you needs something added to the official API that you can otherwise only get from the state field.

Thanks for asking. Between resolved and wrapping value(fut) in a tryCatch I'm able to handle errors without needing any additions to the API.

@keithing keithing closed this as completed Feb 5, 2018
@wlandau
Copy link

wlandau commented Feb 5, 2018

Naive question: is it safe to directly reference fut$state directly, or do you recommend a safer user-side API function similar to value()? I am planning a manual job scheduler for drake that will eventually replace the existing stage-based parallelism, and I am interested in monitoring jobs using future alone. It would be nice to not have to use drake's cache to monitor workers.

@wlandau
Copy link

wlandau commented Feb 6, 2018

Never mind: I should have requested state() in #172.

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

No branches or pull requests

3 participants