title: How to idiomatically end an Asyncio Operation in Python
body: <p>I'm working on code where I have a long running shell command whose output is sent to disk. This command will generate hundreds of GBs per file. I have successfully written code that calls this command asynchronously and successfully yields control (awaits) for it to complete.</p>
  
  <p>I also have code that can asynchronously read that file as it is being written to so that I can process the data contained therein. The problem I'm running into is that I can't find a way to stop the file reader once the shell command completes. </p>
  
  <p>I guess I'm looking for some sort of interrupt I can pass into my writer function once the shell command ends that I can use to tell it to close the file and wrap up the event loop. </p>
  
  <p>Here is my writer function. Right now, it runs forever waiting for new data to be written to the file.</p>
  
  <pre><code>import asyncio
  
  PERIOD = 0.5
  
  async def readline(f):
      while True:
          data = f.readline()
          if data:
              return data
          await asyncio.sleep(PERIOD)
  
  async def read_zmap_file():
      with open('/data/largefile.json'
          , mode = 'r+t'
          , encoding = 'utf-8'
          ) as f:
          i = 0
          while True:
              line = await readline(f)
              print('{:&gt;10}: {!s}'.format(str(i), line.strip()))
              i += 1
  
  loop = asyncio.get_event_loop()
  loop.run_until_complete(read_zmap_file())
  loop.close()
  </code></pre>
  
  <p>If my approach is off, please let me know. I'm relatively new to asynchronous programming. Any help would be appreciated.</p>