title: Writing files asynchronously
body: <p>I've been trying to create a server-process that receives an input file path and an output path from client processes asynchronously. The server does some database-reliant transformations, but for the sake of simplicity let's assume it merely puts everything to the upper case. Here is a toy example of the server:</p>
  
  <pre><code>import asyncio
  import aiofiles as aiof
  import logging
  import sys
  
  
  ADDRESS = ("localhost", 10000)
  
  logging.basicConfig(level=logging.DEBUG,
                      format="%(name)s: %(message)s",
                      stream=sys.stderr)
  
  log = logging.getLogger("main")
  loop = asyncio.get_event_loop()
  
  
  async def server(reader, writer):
      log = logging.getLogger("process at {}:{}".format(*ADDRESS))
      paths = await reader.read()
      in_fp, out_fp = paths.splitlines()
      log.debug("connection accepted")
      log.debug("processing file {!r}, writing output to {!r}".format(in_fp, out_fp))
      async with aiof.open(in_fp, loop=loop) as inp, aiof.open(out_fp, "w", loop=loop) as out:
          async for line in inp:
              out.write(line.upper())
          out.flush()
      writer.write(b"done")
      await writer.drain()
      log.debug("closing")
      writer.close()
      return
  
  
  factory = asyncio.start_server(server, *ADDRESS)
  server = loop.run_until_complete(factory)
  log.debug("starting up on {} port {}".format(*ADDRESS))
  
  try:
      loop.run_forever()
  except KeyboardInterrupt:
      pass
  finally:
      log.debug("closing server")
      server.close()
      loop.run_until_complete(server.wait_closed())
      log.debug("closing event loop")
      loop.close()
  </code></pre>
  
  <p>The client:</p>
  
  <pre><code>import asyncio
  import logging
  import sys
  import random
  
  ADDRESS = ("localhost", 10000)
  MESSAGES = ["/path/to/a/big/file.txt\n", 
              "/output/file_{}.txt\n".format(random.randint(0, 99999))]
  
  logging.basicConfig(level=logging.DEBUG,
                      format="%(name)s: %(message)s",
                      stream=sys.stderr)
  
  log = logging.getLogger("main")
  loop = asyncio.get_event_loop()
  
  async def client(address, messages):
      log = logging.getLogger("client")
      log.debug("connecting to {} port {}".format(*address))
      reader, writer = await asyncio.open_connection(*address)
      writer.writelines([bytes(line, "utf8") for line in messages])
      if writer.can_write_eof():
          writer.write_eof()
      await writer.drain()
  
      log.debug("waiting for response")
      response = await reader.read()
      log.debug("received {!r}".format(response))
      writer.close()
      return
  
  
  try:
      loop.run_until_complete(client(ADDRESS, MESSAGES))
  finally:
      log.debug("closing event loop")
      loop.close()
  </code></pre>
  
  <p>I activated the server and several clients at once. The server's logs:</p>
  
  <pre><code>asyncio: Using selector: KqueueSelector
  main: starting up on localhost port 10000
  process at localhost:10000: connection accepted
  process at localhost:10000: processing file b'/path/to/a/big/file.txt', writing output to b'/output/file_79609.txt'
  process at localhost:10000: connection accepted
  process at localhost:10000: processing file b'/path/to/a/big/file.txt', writing output to b'/output/file_68917.txt'
  process at localhost:10000: connection accepted
  process at localhost:10000: processing file b'/path/to/a/big/file.txt', writing output to b'/output/file_2439.txt'
  process at localhost:10000: closing
  process at localhost:10000: closing
  process at localhost:10000: closing
  </code></pre>
  
  <p>All clients print this:</p>
  
  <pre><code>asyncio: Using selector: KqueueSelector
  client: connecting to localhost port 10000
  client: waiting for response
  client: received b'done'
  main: closing event loop
  </code></pre>
  
  <p>The output files are created, but they remain empty. I believe they are not being flushed. Any way I can fix it? </p>