[docs]defregister(self,api_address):withself.lock:ifself.socketisNone:self.address=api_addresshost,s_port=api_address.split("//")[-1].split(":")self.port=int(s_port)context=zmq.Context()self.socket=context.socket(zmq.PUB)system=platform.system()ifsystem=="Linux"orsystem=="Darwin":forportin[25000,25001]:# Use lsof to find the process ID (PID) using the specified portcommand=f"lsof -i :{port}"result=subprocess.run(command,shell=True,capture_output=True,text=True)# Extract the PID from the lsof outputforlineinresult.stdout.splitlines()[1:]:parts=line.split()iflen(parts)>1:pid=parts[1]response=input(f"Found process with PID {pid} already using {port}, but it is needed by Pymepix. Pymepix may not function properly if this port isn't available. Terminate that process? (y/N): ").strip().lower()ifresponse=='y':print(f"Terminating process {pid} on port {port}")# Terminate processwhileTrue:try:os.kill(int(pid),signal.SIGTERM)time.sleep(1)exceptOSErrorase:ife.errno==errno.ESRCH:# No such processprint(f"Process with PID {pid} has exited.")breakelife.errno==errno.EPERM:# Permission deniedprint(f"No permission to check process with PID {pid}.")breakelse:raiseelse:#Find a way to check for windows (already found, but untested)print(f"Cannot close open ports for operating system: {system}. If required ports are in use there may be difficulties. Rebooting the system may be required.")self.socket.bind(f"tcp://{host}:{s_port}")