class_docstr: PIDServer tracks PIDs reported by a set of pid_clients which connect to it.
  
  PIDServer.run() will return when all pid_clients have reported a graceful shutdown and have
  exited, or it will raise an exception if any pids disappear without reporting a graceful
  shutdown.
  
  PIDServer lets an sshd-based launch layer keep track of its worker processes, even when the
  worker processes aren't proper child processes.
class_docstr_tok: ["PIDServer","tracks","PIDs","reported","by","a","set","of","pid_clients","which","connect","to","it",".","PIDServer.run","(",")","will","return","when","all","pid_clients","have","reported","a","graceful","shutdown","and","have","exited",",","or","it","will","raise","an","exception","if","any","pids","disappear","without","reporting","a","graceful","shutdown",".","PIDServer","lets","an","sshd-based","launch","layer","keep","track","of","its","worker","processes",",","even","when","the","worker","processes","are","n't","proper","child","processes","."]
docstr: Handle an event on a listener socket (aka, accept a connection).
docstr_tok: ["Handle","an","event","on","a","listener","socket","(","aka",",","accept","a","connection",")","."]
code: def handle_listener(self, mask: int) -> None:
      """
      Handle an event on a listener socket (aka, accept a connection).
      """
      assert self.sel
      assert self.listener
      if mask & selectors.EVENT_READ:
          conn, _ = self.listener.accept()
          # We never write anything.
          conn.shutdown(socket.SHUT_WR)
          # First, receive the initial PID for this conn.  Should be nearly instant.
          buf = b""
          while b"\n" not in buf:
              data = conn.recv(4096)
              if not data:
                  raise ValueError("pid_client did not deliver a PID!")
              buf += data
          pid_buf, data_buf = buf.split(b"\n", 1)
          pid = int(pid_buf)
          self.pids.append(pid)
          self.conns[conn] = pid
          # Now listen for this connection to gracefully shut down (eventually)
          conn.setblocking(False)
          self.sel.register(conn, selectors.EVENT_READ)
          if len(self.pids) == self.num_clients:
              # That the last connection, close the listener.
              self.sel.unregister(self.listener)
              self.listener.close()
              self.listener = None
          if data_buf:
              # We received a message in the same packet as the PID, simulate an EVENT_READ.
              self.handle_conn(conn, mask=0, data=data_buf)
      else:
          raise ValueError("listener failed")
  
imports: import logging
  import os
  import selectors
  import signal
  import socket
  import subprocess
  import time
  from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  import determined
label_desc: Use image node_id 6 for calling the PIDServer obj's underlying member method code with example usage: obj.handle_listener(mask) without return types