title: python's asyncio and sharing socket among worker processes
body: <p>Is it possible to share a socket amongst several worker processes using python's asyncio module? </p>
  
  <p>Below is an example code that starts a server listening on port 2000. When a connection is established, and the client sends the string "S", the server starts sending data to the client. But all this happens only on one cpu core. How could I rewrite this example to take advantage of all the cpu cores? I took a look at asyncio subprocess module, but am not sure if I can use it to share the socket so that the server can simultaneously accept connections from multiple worker processes in parallel.</p>
  
  <pre><code>import asyncio
  import datetime
  
  clients = []
  
  class MyServerProtocol(asyncio.Protocol):
  
      def connection_made(self, transport):
          self.transport = transport
          self.peername = transport.get_extra_info("peername")
          print("connection_made: {}".format(self.peername))
          clients.append(self)
  
  
      @asyncio.coroutine
      def send_data_stream(self):
          while True:
              yield from asyncio.sleep(3)
              if self in clients:
                  self.transport.write("{} {}\r\n".format('Endless stream of information', str(datetime.datetime.now())).encode())
                  print("sent data to: {}".format(self.peername))
              else:
                  break 
  
  
      def data_received(self, data):
          print("data_received: {}".format(data.decode()))
          received = data.decode()
          if received == "S":
              asyncio.Task(self.send_data_stream())
  
  
      def connection_lost(self, ex):
          print("connection_lost: {}".format(self.peername))
          clients.remove(self)
  
  
  if __name__ == '__main__':
  
      print("starting up..")
  
      loop = asyncio.get_event_loop()
      asyncio.set_event_loop(loop)
      coro = loop.create_server(MyServerProtocol, port=2000)
      server = loop.run_until_complete(coro)
  
      for socket in server.sockets:
          print("serving on {}".format(socket.getsockname()))
  
      loop.run_forever()
  </code></pre>