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:
method = getattr(target, obj["name"])
ret = method(*obj["args"], **obj["kwargs"])
if inspect.iscoroutine(ret):
ret = await ret
obj = {"status": "ok", "ret": ret}
else:
raise ValueError("Unknown action: {}"

View File

@ -43,6 +43,8 @@ class RPCCase(unittest.TestCase):
try:
test_object_back = remote.echo(test_object)
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):
remote.non_existing_method()
remote.terminate()
@ -68,6 +70,8 @@ class RPCCase(unittest.TestCase):
try:
test_object_back = await remote.echo(test_object)
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):
await remote.non_existing_method()
await remote.terminate()
@ -105,6 +109,10 @@ class Echo:
def echo(self, x):
return x
async def async_echo(self, x):
await asyncio.sleep(0.01)
return x
def run_server():
loop = asyncio.new_event_loop()