[Java] Add getAsyncContext and setAsyncContext API (#6439)

* Add getAsyncContext and setAsyncContext API

* address comment

* fix bug

* Add test case
This commit is contained in:
Kai Yang
2019-12-19 18:08:58 +08:00
committed by Hao Chen
parent 7e2addb424
commit 3bb680a719
6 changed files with 84 additions and 6 deletions
+20 -2
View File
@@ -119,7 +119,25 @@ public final class Ray extends RayCall {
}
/**
* If users want to use Ray API in there own threads, they should wrap their {@link Runnable}
* If users want to use Ray API in their own threads, call this method to get the async context
* and then call {@link #setAsyncContext} at the beginning of the new thread.
*
* @return The async context.
*/
public static Object getAsyncContext() {
return runtime.getAsyncContext();
}
/**
* Set the async context for the current thread.
* @param asyncContext The async context to set.
*/
public static void setAsyncContext(Object asyncContext) {
runtime.setAsyncContext(asyncContext);
}
/**
* If users want to use Ray API in their own threads, they should wrap their {@link Runnable}
* objects with this method.
*
* @param runnable The runnable to wrap.
@@ -130,7 +148,7 @@ public final class Ray extends RayCall {
}
/**
* If users want to use Ray API in there own threads, they should wrap their {@link Callable}
* If users want to use Ray API in their own threads, they should wrap their {@link Callable}
* objects with this method.
*
* @param callable The callable to wrap.
@@ -143,6 +143,10 @@ public interface RayRuntime {
RayPyActor createPyActor(String moduleName, String className, Object[] args,
ActorCreationOptions options);
Object getAsyncContext();
void setAsyncContext(Object asyncContext);
/**
* Wrap a {@link Runnable} with necessary context capture.
* @param runnable The runnable to wrap.