pc_rpc: factor out asyncio server code

This commit is contained in:
Sebastien Bourdeauducq 2014-10-27 20:37:37 +08:00
parent 1e5b05d4ec
commit 2946fa58b8
2 changed files with 23 additions and 11 deletions

View File

@ -10,7 +10,7 @@ import asyncio
from scipy import interpolate
import numpy as np
from artiq.management.pc_rpc import Server
from artiq.management.pc_rpc import simple_server_loop
logger = logging.getLogger("Pdq2")
@ -355,16 +355,7 @@ def main():
dev = Pdq2(serial=args.serial)
try:
loop = asyncio.get_event_loop()
try:
server = Server(dev)
loop.run_until_complete(server.start(args.bind, args.port))
try:
loop.run_forever()
finally:
loop.run_until_complete(server.stop())
finally:
loop.close()
simple_server_loop(dev, args.bind, args.port)
finally:
dev.close()

View File

@ -158,3 +158,24 @@ class Server:
writer.write(line.encode())
finally:
writer.close()
def simple_server_loop(target, host, port):
"""Runs a server until an exception is raised (e.g. the user hits Ctrl-C).
:param target: Object providing the RPC methods to be exposed to the
client.
:param host: Bind address of the server.
:param port: TCP port to bind to.
"""
loop = asyncio.get_event_loop()
try:
server = Server(target)
loop.run_until_complete(server.start(host, port))
try:
loop.run_forever()
finally:
loop.run_until_complete(server.stop())
finally:
loop.close()