-
Hi Everyone, I get a EndOfStream raised exception when running the following code. Code snippet: from anyio import (
TASK_STATUS_IGNORED,
create_task_group,
connect_tcp,
create_tcp_listener,
run,
)
from anyio.abc import TaskStatus
from sums_classes import SUMS_Requests
...
async def sums_handler(stream):
sr = SUMS_Requests(stream, log)
await sr.ahandle(stream) <--all the work is done in this method
async def sum_server(
port: int, *, task_status: TaskStatus[None] = TASK_STATUS_IGNORED
):
async with await create_tcp_listener(
local_host="127.0.0.1", local_port=port
) as listener:
task_status.started()
await listener.serve(sums_handler)
async def sums_main():
async with create_task_group as tg:
await tg.start(sum_server, 6002)
async with await connect_tcp("127.0.0.1", 6002) as stream:
log.writeInfo(['sums_main task group started...'])
try:
run(sums_main)
except KeyboardInterrupt:
raise KeyboardInterrupt
class SUMS_Requests(SUMSRequest):
def __init__(self, stream, log):
self.stream = stream
self.log = log
self.setup() <--method that does some initialization
return None
async def ahandle(self, stream):
self.stream = stream
while 1:
msg_len = await self.stream.receive(8) <---read the first 8 bytes in the stream. this is where EndOfStream is raised
if len(msg_len) == 8:
break
.... lots more code here processing the 4 other messages that arrive from the server
Do I need to use a SocketListener, or raise a custom exception that is passed when no data is there. The use case is this python script needs to run continuously waiting for messages coming from a server code, processing them, and sending the results back to the server, then wait until new messages come in again. Thanks, --Ed |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
As the documentation explains, |
Beta Was this translation helpful? Give feedback.
-
OK, I've resolved the issue of the Segmentation fault. The Segmentation fault was due to a bug in the legacy C code where the requests to the Python script originate. Fixing the bug in the legacy C code then allowed the Python script to run continuously when the socket is readable, and wait for an indefinite period (which can vary from a few seconds, to up to 10 mins) as the legacy codebase works in it's caching DB and queues. For the record, here's the key snippet of AnyIO code that works with the legacy C code driver:
The main class SUMS_Requests snippet is:
To simplify the ahandle method, two new methods are added, getLength and getMessage:
Three points should be emphasized:
Observation: In testing SocketAttribute, I found that the example for typed attributes failed, specifically importing TypedAttribute failed. I don't see a class statement for TypedAttribute in the AnyIO module _core/_typedattr.py |
Beta Was this translation helpful? Give feedback.
OK, I've resolved the issue of the Segmentation fault. The Segmentation fault was due to a bug in the legacy C code where the requests to the Python script originate. Fixing the bug in the legacy C code then allowed the Python script to run continuously when the socket is readable, and wait for an indefinite period (which can vary from a few seconds, to up to 10 mins) as the legacy codebase works in it's caching DB and queues.
For the record, here's the key snippet of AnyIO code that works with the legacy C code driver: