title: Long delay in using asyncio and websockets in Python 3
body: <p>I am experiencing a long (3-hour) delay (EDIT: the delay is brief at first and then gets longer throughout the day) in processing data pushed from a websocket server to my client.py. I know that it is not delayed by the server.</p>
  
  <p>For example every 5 seconds I see the keep_alive log-event and its respective timestamp. So that runs smoothly. <strong>But when I see a data frame processed in logs is actually 3 hours <em>after</em> when the server sent it.</strong> Am I doing something to delay this process?  </p>
  
  <p>Am I calling my coroutine 'keep_alive' correctly? keep_alive is just a message to the server to keep the connection alive. The server echos the message back. Also am I logging too much? Could that be delaying the processing (I don't think so since I'm seeing the logging events occur right away). </p>
  
  <pre><code>async def keep_alive(websocket):
                  """
                   This only needs to happen every 30 minutes. I currently have it set to every 5 seconds.
                  """
                  await websocket.send('Hello')   
                  await asyncio.sleep(5)
  
  async def open_connection_test():
      """
      Establishes web socket (WSS). Receives data and then stores in csv.
      """
      async with websockets.connect( 
              'wss://{}:{}@localhost.urlname.com/ws'.format(user,pswd), ssl=True, ) as websocket:
          while True:    
              """
              Handle message from server.
              """
              message = await websocket.recv()
              if message.isdigit():
                  # now = datetime.datetime.now()
                  rotating_logger.info ('Keep alive message: {}'.format(str(message)))
              else:
                  jasonified_message = json.loads(message)
                  for key in jasonified_message:
                      rotating_logger.info ('{}: \n\t{}\n'.format(key,jasonified_message[key]))    
                  """
                  Store in a csv file.
                  """
                  try:            
                      convert_and_store(jasonified_message)
                  except PermissionError:
                      convert_and_store(jasonified_message, divert = True)                        
              """
              Keep connection alive.
              """            
              await keep_alive(websocket)
  
  """
  Logs any exceptions in logs file.
  """
  try:
      asyncio.get_event_loop().run_until_complete(open_connection())
  except Exception as e:
      rotating_logger.info (e)
  </code></pre>
  
  <p>EDIT:
  From the <a href="https://websockets.readthedocs.io/en/stable/api.html#module-websockets.protocol" rel="noreferrer">documentation</a> - I'm thinking that this might have something to do with it - but I haven't connected the dots.</p>
  
  <blockquote>
    <p>The max_queue parameter sets the maximum length of the queue that
    holds incoming messages. The default value is 32. 0 disables the
    limit. Messages are added to an in-memory queue when they’re received;
    then recv() pops from that queue. In order to prevent excessive memory
    consumption when messages are received faster than they can be
    processed, the queue must be bounded. If the queue fills up, the
    protocol stops processing incoming data until recv() is called. In
    this situation, various receive buffers (at least in asyncio and in
    the OS) will fill up, then the TCP receive window will shrink, slowing
    down transmission to avoid packet loss.</p>
  </blockquote>
  
  <p>EDIT 9/28/2018: I'm testing it without the keep-alive message and that doesn't seem to be the issue. Could it be related to the convert_and_store() function? Does this need to be async def and then awaited as well?</p>
  
  <pre><code>def convert_and_store(data, divert = False, test = False):
      if test:
          data = b
      fields = data.keys()
      file_name =  parse_call_type(data, divert = divert)
      json_to_csv(data, file_name, fields)
  </code></pre>
  
  <p>EDIT 10/1/2018: It seems that both the keep-alive message and convert_and_store are both at issue; if I extend the keep-alive message to 60 seconds - then convert_and_store will run <strong>only once per 60 seconds</strong>. So convert_and_store is waiting on the keep_alive()...</p>