server.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """module server."""
  2. from __future__ import annotations
  3. import json
  4. from typing import TYPE_CHECKING, Any
  5. from wandb import util
  6. if TYPE_CHECKING:
  7. from wandb.sdk.wandb_settings import Settings
  8. class Server:
  9. def __init__(
  10. self,
  11. settings: Settings,
  12. ) -> None:
  13. from wandb.apis import InternalApi
  14. self._api = InternalApi(default_settings=settings)
  15. self._viewer: dict[str, Any] = {}
  16. self._flags: dict[str, Any] = {}
  17. self._settings: Settings = settings
  18. def query_with_timeout(self, timeout: int | float = 5) -> None:
  19. if self._settings.x_disable_viewer:
  20. return
  21. async_viewer = util.async_call(self._api.viewer_server_info, timeout=timeout)
  22. try:
  23. viewer_tuple, viewer_thread = async_viewer()
  24. except Exception:
  25. return
  26. if viewer_thread.is_alive():
  27. # this is likely a DNS hang
  28. return
  29. # TODO(jhr): should we kill the thread?
  30. self._viewer, self._serverinfo = viewer_tuple
  31. self._flags = json.loads(self._viewer.get("flags", "{}"))
  32. @property
  33. def viewer(self) -> dict[str, Any]:
  34. """Returns information about the currently authenticated user.
  35. If the API key is valid, the following is returned:
  36. - id
  37. - entity
  38. - username
  39. - flags
  40. - teams
  41. If the API key is not valid or the server is not reachable,
  42. an empty dict is returned.
  43. """
  44. if not self._viewer and not self._settings._offline:
  45. self.query_with_timeout()
  46. return self._viewer