mirror of
https://github.com/wassname/ray.git
synced 2026-06-29 11:01:06 +08:00
[xlang] Cross language Python support (#6709)
This commit is contained in:
@@ -10,11 +10,14 @@ import org.ray.api.Ray;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.RayPyActor;
|
||||
import org.ray.api.TestUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class CrossLanguageInvocationTest extends BaseMultiLanguageTest {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CrossLanguageInvocationTest.class);
|
||||
private static final String PYTHON_MODULE = "test_cross_language_invocation";
|
||||
|
||||
@Override
|
||||
@@ -46,6 +49,12 @@ public class CrossLanguageInvocationTest extends BaseMultiLanguageTest {
|
||||
Assert.assertEquals(res.get(), "Response from Python: hello".getBytes());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPythonCallJavaFunction() {
|
||||
RayObject res = Ray.callPy(PYTHON_MODULE, "py_func_call_java_function", "hello".getBytes());
|
||||
Assert.assertEquals(res.get(), "[Python]py_func -> [Java]bytesEcho -> hello".getBytes());
|
||||
}
|
||||
|
||||
@Test(groups = {"directCall"})
|
||||
public void testCallingPythonActor() {
|
||||
// Python worker doesn't support direct call yet.
|
||||
@@ -54,4 +63,32 @@ public class CrossLanguageInvocationTest extends BaseMultiLanguageTest {
|
||||
RayObject res = Ray.callPy(actor, "increase", "1".getBytes());
|
||||
Assert.assertEquals(res.get(), "2".getBytes());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPythonCallJavaActor() {
|
||||
RayObject res = Ray.callPy(PYTHON_MODULE, "py_func_call_java_actor", "1".getBytes());
|
||||
Assert.assertEquals(res.get(), "Counter1".getBytes());
|
||||
}
|
||||
|
||||
public static byte[] bytesEcho(byte[] value) {
|
||||
// This function will be called from test_cross_language_invocation.py
|
||||
String valueStr = new String(value);
|
||||
LOGGER.debug(String.format("bytesEcho called with: %s", valueStr));
|
||||
return ("[Java]bytesEcho -> " + valueStr).getBytes();
|
||||
}
|
||||
|
||||
public static class TestActor {
|
||||
public TestActor(byte[] v) {
|
||||
value = v;
|
||||
}
|
||||
|
||||
public byte[] concat(byte[] v) {
|
||||
byte[] c = new byte[value.length + v.length];
|
||||
System.arraycopy(value, 0, c, 0, value.length);
|
||||
System.arraycopy(v, 0, c, value.length, v.length);
|
||||
return c;
|
||||
}
|
||||
|
||||
private byte[] value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,25 @@ def py_func(value):
|
||||
return b"Response from Python: " + value
|
||||
|
||||
|
||||
@ray.remote
|
||||
def py_func_call_java_function(value):
|
||||
assert isinstance(value, bytes)
|
||||
f = ray.java_function("org.ray.api.test.CrossLanguageInvocationTest",
|
||||
"bytesEcho")
|
||||
r = f.remote(value)
|
||||
return b"[Python]py_func -> " + ray.get(r)
|
||||
|
||||
|
||||
@ray.remote
|
||||
def py_func_call_java_actor(value):
|
||||
assert isinstance(value, bytes)
|
||||
c = ray.java_actor_class(
|
||||
"org.ray.api.test.CrossLanguageInvocationTest$TestActor")
|
||||
java_actor = c.remote(b"Counter")
|
||||
r = java_actor.concat.remote(value)
|
||||
return ray.get(r)
|
||||
|
||||
|
||||
@ray.remote
|
||||
class Counter(object):
|
||||
def __init__(self, value):
|
||||
|
||||
Reference in New Issue
Block a user