tianjiqx commented on issue #13745:
URL: https://github.com/apache/arrow/issues/13745#issuecomment-1200093916

   Probably didn't understand how to use SO_REUSEPORT. Sometimes it is possible 
to bind to the same port, but the created clients always only connect to the 
same server.
   
   ```
   @contextlib.contextmanager
   def _init_grpc_port(grpc_port):
       """Initialize grpc port for multiprocessing."""
       # Reference code.
       # https://github.com/grpc/grpc/issues/17659
   
       socket = py_socket.socket(py_socket.AF_INET6, py_socket.SOCK_STREAM)
       # in fact, py_socket.SOL_SOCKET = 65535
       # print(py_socket.SOL_SOCKET)
   
       socket.setsockopt(py_socket.SOL_SOCKET, py_socket.SO_REUSEADDR, 1)
       if socket.getsockopt(py_socket.SOL_SOCKET, py_socket.SO_REUSEADDR) == 0:
           raise RuntimeError("Failed to set SO_REUSEADDR.")
       socket.setsockopt(py_socket.SOL_SOCKET, py_socket.SO_REUSEPORT, 1)
       if socket.getsockopt(py_socket.SOL_SOCKET, py_socket.SO_REUSEPORT) == 0:
           raise RuntimeError("Failed to set SO_REUSEPORT.")
       socket.bind(('', grpc_port))
       try:
           yield socket.getsockname()[1]
       finally:
           socket.close()
   
   def main():
       with _init_grpc_port(5005) as port:
           pass
   
       for i in range(2):
           p = Process(target=server.start, args=(i, ))
           # if set time.sleep(1) then always throw "os_error":"Address already 
in use","syscall":"bind"
           p.start()
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to