Skip to content

Commit 1e6c2a2

Browse files
committed
v3/as_drivers/client_server/userver Add client timeout.
1 parent f874ac4 commit 1e6c2a2

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

v3/as_drivers/client_server/userver.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# userver.py Demo of simple uasyncio-based echo server
22

33
# Released under the MIT licence
4-
# Copyright (c) Peter Hinch 2019
4+
# Copyright (c) Peter Hinch 2019-2020
55

66
import usocket as socket
77
import uasyncio as asyncio
@@ -11,11 +11,17 @@
1111

1212
class Server:
1313

14-
async def run(self, port=8123):
14+
def __init__(self, host='0.0.0.0', port=8123, backlog=5, timeout=20):
15+
self.host = host
16+
self.port = port
17+
self.backlog = backlog
18+
self.timeout = timeout
19+
20+
async def run(self):
1521
print('Awaiting client connection.')
1622
self.cid = 0
1723
asyncio.create_task(heartbeat(100))
18-
self.server = await asyncio.start_server(self.run_client, '0.0.0.0', port)
24+
self.server = await asyncio.start_server(self.run_client, self.host, self.port, self.backlog)
1925
while True:
2026
await asyncio.sleep(100)
2127

@@ -24,7 +30,10 @@ async def run_client(self, sreader, swriter):
2430
print('Got connection from client', self.cid)
2531
try:
2632
while True:
27-
res = await sreader.readline()
33+
try:
34+
res = await asyncio.wait_for(sreader.readline(), self.timeout)
35+
except asyncio.TimeoutError:
36+
res = b''
2837
if res == b'':
2938
raise OSError
3039
print('Received {} from client {}'.format(ujson.loads(res.rstrip()), self.cid))

0 commit comments

Comments
 (0)