title: Python asyncio - heartbeat() method not writing to stream
body: <p>I trying to create a proof of concept with Python 3 <em>asyncio</em>, implementing a client that sends <em>heartbeats</em> periodically to a server in order to keep the connection alive.</p>
  <p>Note that the server is simply an echo server and doesn't close the connection. But it is important that the client is able to send a heartbeat periodically.</p>
  <p>Here is the current implementation:</p>
  <h2>stream_client.py</h2>
  <pre><code>import asyncio
  
  
  class StreamClient:
  
      def __init__(self, heartbeat_int, loop=None):
          self.heartbeat_int = heartbeat_int
  
          if loop is not None:
              self.loop = loop
          else:
              self.loop = asyncio.get_event_loop()
  
      def start(self):
          &quot;&quot;&quot; start manages the event loop, but it is not a coroutine &quot;&quot;&quot;
  
          self.loop.run_until_complete(self.get_client())
          self.loop.create_task(self.start_timed_session())
          msg = self.loop.run_until_complete(self.logon('hello'))
  
          if msg == 'hello':
              try:
                  self.loop.run_forever()
              except KeyboardInterrupt:
                  print('Closing connection with server...')
                  self.writer.close()
                  self.loop.close()
          else:
              print('Logon unsuccessful, closing connection with server...')
              self.writer.close()
              self.loop.close()
  
      @asyncio.coroutine
      def get_client(self):
          self.reader, self.writer = yield from asyncio.open_connection(
              '127.0.0.1',
              9871,
              loop=self.loop
          )
          print('Connection established at &quot;localhost:9871&quot;')
  
      @asyncio.coroutine
      def timed_session(self):
          yield from asyncio.sleep(self.heartbeat_int)
          self.loop.create_task(self.start_timed_session())
  
      @asyncio.coroutine
      def start_timed_session(self):
          heartbeat_task = self.loop.create_task(self.timed_session())
          heartbeat_task.add_done_callback(self.heartbeat)
  
      @asyncio.coroutine
      def logon(self, msg):
          print('Sending message:', msg)
          self.writer.write(msg.encode())
          data = yield from self.reader.read(15)
          resp = data.decode()
          print('Data received:', resp)
          return resp
  
      def heartbeat(self, fut):
          &quot;&quot;&quot;
          This is future's callback:
              1) Can't be a coroutine
              2) Takes a future as an argument
          &quot;&quot;&quot;
  
          print('&gt; Sending heartbeat...')
          self.writer.write('heartbeat'.encode())
  
  
  # Start the client
  
  client = StreamClient(5)
  client.start()
  </code></pre>
  <h2>stream_server.py</h2>
  <pre><code>import asyncio
  
  
  class StreamServer:
  
      def __init__(self, loop=None):
          if loop is not None:
              self.loop = loop
          else:
              self.loop = asyncio.get_event_loop()
  
      @asyncio.coroutine
      def server_handler(self, reader, writer):
          data = yield from reader.read(15)
          msg = data.decode()
          if data is not 'bye':
              print('Received data: &quot;{}&quot; from {}'.format(msg, writer.get_extra_info('peername')))
              print('Echoing the message...')
              writer.write(data)
              yield from writer.drain()
          else:
              print('Received data: &quot;{}&quot; from {}'.format(
                      data,
                      writer.get_extra_info('peername')
                  )
              )
              print('Closing the connection...')
              writer.close()
  
  
  loop = asyncio.get_event_loop()
  stream_server = StreamServer(loop)
  
  coro_server = asyncio.start_server(
      stream_server.server_handler,
      '127.0.0.1',
      9871,
      loop=stream_server.loop
  )
  
  server = loop.run_until_complete(coro_server)
  
  print('Listening on:', server.sockets[0].getsockname())
  
  try:
      loop.run_forever()
  except KeyboardInterrupt:
      pass
  
  print('Closing server')
  server.close()
  loop.run_until_complete(server.wait_closed())
  loop.close()
  </code></pre>
  <h1>Question:</h1>
  <p>On the <code>heartbeat()</code> method the line <code>self.writer.write('heartbeat'.encode())</code> seems to never be executed.</p>
  <p>How can I get it to work?</p>