title: Python Correct Way to Use asyncio streams to open a connection, send and receive multiple transmissions, then close connection gracefully
body: <p>I am asking where either my though process or my code is incorrect relative to using asyncio client streams to send data and receive responses from a server. When I call the method that disconnects the client an exception is thrown.  I am learning python asyncio and ran across exceptions during testing trying to close the client connection. I am trying to 1). Create a client connection to a server 2). leave the client connection open so that it can be used across multiple send/receive cycles 3). close the client connection gracefully when complete.</p>
  
  <p>This is the class that contains the asyncio methods to create the stream writer.</p>
  
  <pre><code>  class hl7_client_sender:
        SB = b'\x1B'
        EB = b'\x1C'
        CR = b'\x0D'
  
        def __init__(self,address,port,timeout=-1,retry=3.0):
            self._resend=0
            self._timeout= timeout
            self._retry = retry
            #self._reader, self._writer = await asyncio.open_connection(address,port)
            self._address = address
            self._port = port
            self._writer = None
            self._reader = None
  
  async def connect(self):
      self._reader, self._writer = await asyncio.open_connection(self._address,self._port)
  
  async def disconnect(self):
      await self._writer.wait_closed()
  </code></pre>
  
  <p>and this is the code in my driver where the exception occurs during the call to disconnect</p>
  
  <pre><code>  #test send and respond
    import asyncio
    import string
    import unicodedata
    import simple_hl7_client
    import time
    ##open a connectino sleep 5 seconds then close###
    myclient = simple_hl7_client.hl7_client_sender('192.168.226.128',54321)
    asyncio.run(myclient.connect())
    time.sleep(3)
    asyncio.run(myclient.disconnect())
  </code></pre>
  
  <p>The exception occurs during the call to asycnio.run(myclient.disconnect())</p>
  
  <p>This is the exception:</p>
  
  <pre><code>      Traceback (most recent call last):
          File ".\test_simple_hl7_client.py", line 11, in &lt;module&gt;
     asyncio.run(myclient.disconnect())
     File "C:\Users\billg\AppData\Local\Programs\Python\Python37\lib\asyncio\runners.py",           line 43, in run
        return loop.run_until_complete(main)
      File "C:\Users\billg\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 583, in run_until_complete
        return future.result()
      File "D:\data\FromOldPC\code\ASYNCIOTESTING\simple_hl7_client.py", line 23, in disconnect
        self._writer.close()
      File "C:\Users\billg\AppData\Local\Programs\Python\Python37\lib\asyncio\streams.py", line 317, in close
        return self._transport.close()
      File "C:\Users\billg\AppData\Local\Programs\Python\Python37\lib\asyncio\selector_events.py", line 663, in close
        self._loop.call_soon(self._call_connection_lost, None)
      File "C:\Users\billg\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 687, in call_soon
        self._check_closed()
      File "C:\Users\billg\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 479, in _check_closed
        raise RuntimeError('Event loop is closed')
    RuntimeError: Event loop is closed
  </code></pre>