utils.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import enum
  2. import os
  3. import sys
  4. from typing import TypeVar
  5. import aiohttp
  6. from ray._private.utils import validate_socket_filepath
  7. K = TypeVar("K")
  8. V = TypeVar("V")
  9. class ResponseType(enum.Enum):
  10. HTTP = "http"
  11. STREAM = "stream"
  12. WEBSOCKET = "websocket"
  13. def module_logging_filename(
  14. module_name: str, logging_filename: str, extension: str = ""
  15. ) -> str:
  16. """
  17. Parse logging_filename = STEM EXTENSION,
  18. return STEM _ MODULE_NAME _ EXTENSION
  19. If logging_filename is empty, return empty string.
  20. If extension is empty, use the extension from logging_filename.
  21. Example:
  22. module_name = "TestModule"
  23. logging_filename = "dashboard.log"
  24. STEM = "dashboard"
  25. EXTENSION = ".log"
  26. return "dashboard_TestModule.log"
  27. """
  28. if not logging_filename:
  29. return ""
  30. stem, ext = os.path.splitext(logging_filename)
  31. if not extension:
  32. extension = ext
  33. return f"{stem}_{module_name}{extension}"
  34. def get_socket_path(socket_dir: str, module_name: str) -> str:
  35. socket_path = os.path.join(socket_dir, "dash_" + module_name)
  36. validate_socket_filepath(socket_path)
  37. return socket_path
  38. def get_named_pipe_path(module_name: str, session_name: str) -> str:
  39. return r"\\.\pipe\dash_" + module_name + "_" + session_name
  40. def get_http_session_to_module(
  41. module_name: str, socket_dir: str, session_name: str
  42. ) -> aiohttp.ClientSession:
  43. """
  44. Get the aiohttp http client session to the subprocess module.
  45. """
  46. if sys.platform == "win32":
  47. named_pipe_path = get_named_pipe_path(module_name, session_name)
  48. connector = aiohttp.NamedPipeConnector(named_pipe_path)
  49. else:
  50. socket_path = get_socket_path(socket_dir, module_name)
  51. connector = aiohttp.UnixConnector(socket_path)
  52. return aiohttp.ClientSession(connector=connector)