await process_command(command, pins, out_queue)
-async def event_handler(websocket, path):
+async def event_handler(websocket, path, out_queue):
while True:
event = await out_queue.get()
message = json.dumps(event)
async def handler(pins, out_queue, websocket, path):
command_task = asyncio.create_task(command_handler(websocket, path,
pins, out_queue))
- event_task = asyncio.create_task(event_handler(websocket, path))
+ event_task = asyncio.create_task(event_handler(websocket, path,
+ out_queue))
done, pending = await asyncio.wait(
[command_task, event_task],
return_when=asyncio.FIRST_COMPLETED,
commands = [{'command': 'getallpins'},
{'command': 'setpin', 'pin': 'A2-8', 'value': True},
{'command': 'setpin', 'pin': 'A2-8', 'value': True},
+ {'command': 'getpin', 'pin': 'T2-16'},
{'command': 'setpin', 'pin': 'A2-8', 'value': False},
{'command': 'getpin', 'pin': 'T2-16'}]
for command in commands:
message = json.dumps(command)
await websocket.send(message)
print(f"Sent Command: {message}")
+ await asyncio.sleep(0.1)
async def receive_events(websocket):
async def main(hostname):
- async with websockets.connect(f"ws://{hostname}:80") as websocket:
+ async with websockets.connect(f"ws://{hostname}") as websocket:
command_task = asyncio.create_task(test_commands(websocket))
event_task = asyncio.create_task(receive_events(websocket))
- done, pending = await asyncio.wait(
- [command_task, event_task],
- return_when=asyncio.BOTH_COMPLETED,
- )
- for task in pending:
- task.cancel()
-
+ await command_task
if __name__ == '__main__':
- try:
- asyncio.get_event_loop().run_until_complete(main(sys.argv[1]))
- except KeyboardInterrupt:
- pass
+ asyncio.run(main(sys.argv[1]))