I’m working on an app that reads an input stream from a serial port and uses websockets to display the data on a web page. My plan was to create two asynchronous tasks, one for reading and writing to the serial port, the other to run the web server, and pass data between them using asyncio queues.
The problem I’m having is that I can’t see how to pass the queue as input to the Sanic server. Creating a server using the recommended method for asynchronous I/O does not permit passing keyword arguments like app.run() does:
asyncio.set_event_loop(uvloop.new_event_loop())
loop = asyncio.get_event_loop()
task_queue = asyncio.Queue(loop=loop, maxsize =10)
# this produces "unexpected keyword argument"
# server = app.create_server(host="0.0.0.0", port=8081, task_queue=task_queue)
server = app.create_server(host="0.0.0.0", port=8081)
asyncio.ensure_future(read_serial_port(task_queue))
# can't pass it here either as it's not callable
# asyncio.ensure_future(server(task_queue=task_queue))
asyncio.ensure_future(server)
signal(SIGINT, lambda s, f: loop.stop())
try:
loop.run_forever()
except:
loop.stop()
Any help would be greatly appreciated.