forked from M-Labs/artiq
protocols/pc_rpc: support coroutine methods
This commit is contained in:
parent
e8bd24d539
commit
3edf52232c
@ -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: {}"
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user