I am trying to listen to a TCP server I have no control over (it is a local windows app broadcasting every 500ms on a fixed socket).
I can set up a reader to get a random length packet in blocking mode
import asyncio, telnetlib3, time
TCP_SERVER_ADDRESS = "127.0.0.7"
TCP_SERVER_PORT = 8000
async def get_one_line(reader):
reply1 = []
while True:
c = await reader.read(1)
if not c:
break
if c in ['\r', '\n']:
break
reply1.append(c)
return reply1
async def main():
reader, writer = await telnetlib3.open_connection(TCP_SERVER_ADDRESS, TCP_SERVER_PORT)
writer.write("$SYS,INFO")
count = 0
while True:
reply = []
reply = await get_one_line(reader)
if reply:
print('reply:', ''.join(reply))
del reply[:]
print(count)
count += 1
if count > 10:
break
time.sleep(0.1)
asyncio.run(main())
and it prints "count" once per TCP packet as expected (sorry about the XXX, not my data)
reply: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
2
reply: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
3
reply: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
4
:
reply: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
10
When I tried to wrap get_one_line() in an async function, I wrapped myself firmly around the axle. If I remove the "stop after 10", then it just prints count without receiving anything. If I quit after 10, then all kinds of errors (I assume) because the task was left running.
import asyncio, telnetlib3, time
TCP_SERVER_ADDRESS = "127.0.0.7"
TCP_SERVER_PORT = 8000
async def get_one_line(reader):
reply1 = []
while True:
c = await reader.read(1)
if not c:
break
if c in ['\r', '\n']:
break
reply1.append(c)
return reply1
async def get_nonblocking(reader, callback):
reply2 = await get_one_line(reader)
callback(reply2)
async def main():
reader, writer = await telnetlib3.open_connection(TCP_SERVER_ADDRESS, TCP_SERVER_PORT)
writer.write("$SYS,INFO")
count = 0
while True:
reply = []
getTask = asyncio.create_task(get_nonblocking(reader,reply.append))
if reply:
print('reply:', ''.join(reply))
del reply[:]
print(count)
count += 1
if count > 10:
getTask.cancel()
break
time.sleep(0.1)
asyncio.run(main())
I assume it's that I do not know how to use asyncio and 2 days of reading have not helped. Any help is appreciated.
get_nonblockingin ` asyncio.create_task()`? Since the rest of your loop no longer waits for its execution, it just quickly counts up to 10 (waiting .1 seconds between counts) and then cancels the task.