forked from M-Labs/artiq
1
0
Fork 0

protocols/pc_rpc: support coroutine methods

This commit is contained in:
Sebastien Bourdeauducq 2015-12-06 12:52:41 +08:00
parent e8bd24d539
commit 3edf52232c
2 changed files with 10 additions and 0 deletions

View File

@ -490,6 +490,8 @@ class Server(_AsyncioServer):
else: else:
method = getattr(target, obj["name"]) method = getattr(target, obj["name"])
ret = method(*obj["args"], **obj["kwargs"]) ret = method(*obj["args"], **obj["kwargs"])
if inspect.iscoroutine(ret):
ret = await ret
obj = {"status": "ok", "ret": ret} obj = {"status": "ok", "ret": ret}
else: else:
raise ValueError("Unknown action: {}" raise ValueError("Unknown action: {}"

View File

@ -43,6 +43,8 @@ class RPCCase(unittest.TestCase):
try: try:
test_object_back = remote.echo(test_object) test_object_back = remote.echo(test_object)
self.assertEqual(test_object, test_object_back) self.assertEqual(test_object, test_object_back)
test_object_back = remote.async_echo(test_object)
self.assertEqual(test_object, test_object_back)
with self.assertRaises(pc_rpc.RemoteError): with self.assertRaises(pc_rpc.RemoteError):
remote.non_existing_method() remote.non_existing_method()
remote.terminate() remote.terminate()
@ -68,6 +70,8 @@ class RPCCase(unittest.TestCase):
try: try:
test_object_back = await remote.echo(test_object) test_object_back = await remote.echo(test_object)
self.assertEqual(test_object, test_object_back) self.assertEqual(test_object, test_object_back)
test_object_back = await remote.async_echo(test_object)
self.assertEqual(test_object, test_object_back)
with self.assertRaises(pc_rpc.RemoteError): with self.assertRaises(pc_rpc.RemoteError):
await remote.non_existing_method() await remote.non_existing_method()
await remote.terminate() await remote.terminate()
@ -105,6 +109,10 @@ class Echo:
def echo(self, x): def echo(self, x):
return x return x
async def async_echo(self, x):
await asyncio.sleep(0.01)
return x
def run_server(): def run_server():
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()