python-hl7 icon indicating copy to clipboard operation
python-hl7 copied to clipboard

MLLP using asyncio,the HL7 Receiver occur “Error occurred in main: __aexit__ AttributeError”

Open Ming-Y-ANG opened this issue 2 years ago • 4 comments

Refer to the documentation, I plan to use asyncio to make an HL7 server, the relevant code is as follows:

import aiorun
import asyncio
import hl7
from hl7.mllp import start_hl7_server, HL7StreamReader, HL7StreamWriter
  
async def process_hl7_messages(hl7_reader=HL7StreamReader, hl7_writer=HL7StreamWriter):
      """This will be called every time a socket connects
      with us.
      """
      peername = hl7_writer.get_extra_info("peername")
      print("Connection established {}".format(peername))
      try:
          # We're going to keep listening until the writer
          # is closed. Only writers have closed status.
          #while not hl7_writer.is_closing():
              hl7_message = await hl7_reader.readmessage()
              print("Received message:{}".format(hl7_message))
              # Now let's send the ACK and wait for the
              # writer to drain
              hl7_writer.writemessage(hl7_message.create_ack())
              await hl7_writer.drain()
      except asyncio.IncompleteReadError:
          # Oops, something went wrong, if the writer is not
          # closed or closing, close it.
          #if not hl7_writer.is_closing():
              hl7_writer.close()
              await hl7_writer.wait_closed()
      print("Connection closed {}".format(peername))
      
async def main():
      try:
          # Start the server in a with clause to make sure we
          # close it
          async with await start_hl7_server(
              client_connected_cb=process_hl7_messages, host='10.5.47.17', port=9090
          ) as hl7_server:
              # And now we server forever. Or until we are
              # cancelled...
              await hl7_server.serve_forever()
      
      except asyncio.CancelledError:
          # Cancelled errors are expected
          pass
      except Exception as e:
          print("Error occurred in main:", e, e.__class__.__name__,)
          
aiorun.run(main(), stop_on_unhandled_errors=True)

I commented out : “while not hl7_writer.is_closing():” “if not hl7_writer.is_closing():” since it doesn't seem to be implemented.

environment ubuntu 16.04, Python 3.5.2,when run "python3 server_test.py" the following error occurred: "Error occurred in main: aexit AttributeError" Hope someone can help me with this

Ming-Y-ANG avatar Apr 13 '23 08:04 Ming-Y-ANG

We dropped support for Python 3.5 in v0.4.3 https://github.com/johnpaulett/python-hl7/blob/main/docs/changelog.rst#043---march-2022

I believe there were asyncio improvements since then that we are using.

FYI Python 3.5 was End of Life in 2020 https://endoflife.date/python thus our decision to drop support.

It does look like our doc website is not getting updates for the change log, so I'll look at that.

johnpaulett avatar Apr 13 '23 09:04 johnpaulett

Yes, I got the sample from this site:https://python-hl7.readthedocs.io/en/latest/mllp.html. At the same time, I tried again on Python 3.6.9 and found the same error. I checked some information, this error seems to be that if there is no aenter and aexit method in the context manager passed to the async with expression, python seems to throw an error, but I didn't find a solution

Ming-Y-ANG avatar Apr 13 '23 10:04 Ming-Y-ANG

It seems that there is indeed a problem with the Python version. I tested it on Python 3.8.10 and it work fine

Ming-Y-ANG avatar Apr 13 '23 13:04 Ming-Y-ANG

Great. We're only supporting 3.8+ (and will likely drop 3.8 when it is end-of-lifed this year).

johnpaulett avatar Apr 13 '23 13:04 johnpaulett