title: How to create a parallel loop using aiohttp or asyncio in Python?
body: <p>I would like to use rethinkdb .changes() feature to push some messages to  users. The messages should send without any requests from the users.</p>
  
  <p>I am using rethinkdb with aiohttp and websockets. How it works:</p>
  
  <ol>
  <li>User sends message</li>
  <li>Server puts it into rethinkdb</li>
  <li>What I need: an additional loop uses rethinkdb <code>.changes</code> function to send updates to connected users</li>
  </ol>
  
  <p>This is how I initiate the application:</p>
  
  <pre><code>@asyncio.coroutine
  def init(loop):
      app = Application(loop=loop)
      app['sockets'] = []
      app['susers'] = []
      app.router.add_route('GET', '/', wshandler)
      handler = app.make_handler()
      srv = yield from loop.create_server(handler, '127.0.0.1', 9080)
      print("Server started at http://127.0.0.1:9080")
      return app, srv, handler
  </code></pre>
  
  <p>In the <code>wshandler</code> I have a loop, which processes incoming messages:</p>
  
  <pre><code>@asyncio.coroutine
  def wshandler(request):
      resp = WebSocketResponse()
      if not resp.can_prepare(request):
          return Response(
              body=bytes(json.dumps({"error_code": 401}), 'utf-8'),
              content_type='application/json'
          )
      yield from resp.prepare(request)
      request.app['sockets'].append(resp)
      print('Someone connected')
      while True:
          msg = yield from resp.receive()
          if msg.tp == MsgType.text:
              runCommand(msg, resp, request)
          else:
              break
      request.app['sockets'].remove(resp)
      print('Someone disconnected.')
      return resp
  </code></pre>
  
  <p>How to create a second loop sending messages to the same pool of opened connections? How to make it thread-safe?</p>