title: Trying to implement 2 "threads" using `asyncio` module
body: <p>I've played around with threading before in Python, but decided to give the <code>asyncio</code> module a try, especially since you can cancel a running task, which seemed like a nice detail. However, for some reason, I can't wrap my head around it.</p>
  
  <p>Here's what I wanted to implement (sorry if I'm using incorrect terminology):</p>
  
  <ul>
  <li>a <code>downloader</code> thread that downloads the same file every <em>x</em> seconds, checks its hash against the previous download and saves it if it's different.</li>
  <li>a <code>webserver</code> thread that runs in the background, allowing control (pause, list, stop) of the <code>downloader</code> thread.</li>
  </ul>
  
  <p>I used <code>aiohttp</code> for the webserver.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>class aiotest():
  
      def __init__(self):
          self._dl = None     # downloader future
          self._webapp = None # web server future
          self.init_server()
  
      def init_server(self):
  
          print('Setting up web interface')
          app = web.Application()
          app.router.add_route('GET', '/stop', self.stop)
          print('added urls')
          self._webapp = app
  
      @asyncio.coroutine
      def _downloader(self):
          while True:
              try:
                  print('Downloading and verifying file...')
                  # Dummy sleep - to be replaced by actual code
                  yield from asyncio.sleep(random.randint(3,10))
                  # Wait a predefined nr of seconds between downloads
                  yield from asyncio.sleep(30)
              except asyncio.CancelledError:
                  break
  
      @asyncio.coroutine
      def _supervisor(self):
  
          print('Starting downloader')
          self._dl = asyncio.async(self._downloader())
  
      def start(self):
          loop = asyncio.get_event_loop()
          loop.run_until_complete(self._supervisor())
          loop.close()
  
      @asyncio.coroutine
      def stop(self):
          print('Received STOP')
          self._dl.cancel()
          return web.Response(body=b"Stopping... ")
  </code></pre>
  
  <p>This class is called by:</p>
  
  <pre><code>t = aiotest()
  t.start()
  </code></pre>
  
  <p>This doesn't work of course, and I feel that this is a horrible piece of code.</p>
  
  <p>What's unclear to me:</p>
  
  <ul>
  <li>I stop the <code>downloader</code> in the <code>stop()</code> method, but how would I go about stopping the webserver (e.g. in a <code>shutdown()</code> method)?</li>
  <li>Does the <code>downloader</code> need a new event loop, or can I use the loop returned by <code>asyncio.get_event_loop()</code>?</li>
  <li>Do I really need something like the <code>supervisor</code> for what I'm trying to implement? This seems so clunky. And how do I get <code>supervisor</code> to keep running instead of ending after a single execution as it does now?</li>
  </ul>
  
  <p>One last, more general question: is <code>asyncio</code> supposed to replace the <code>threading</code> module (in the future)? Or does each have its own application?</p>
  
  <p>I appreciate all the pointers, remarks and clarifications!</p>