_greenlets.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright (c) Microsoft Corporation.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. from typing import Tuple
  16. import greenlet
  17. def _greenlet_trace_callback(
  18. event: str, args: Tuple[greenlet.greenlet, greenlet.greenlet]
  19. ) -> None:
  20. if event in ("switch", "throw"):
  21. origin, target = args
  22. print(f"Transfer from {origin} to {target} with {event}")
  23. if os.environ.get("INTERNAL_PW_GREENLET_DEBUG"):
  24. greenlet.settrace(_greenlet_trace_callback)
  25. class MainGreenlet(greenlet.greenlet):
  26. def __str__(self) -> str:
  27. return "<MainGreenlet>"
  28. class RouteGreenlet(greenlet.greenlet):
  29. def __str__(self) -> str:
  30. return "<RouteGreenlet>"
  31. class LocatorHandlerGreenlet(greenlet.greenlet):
  32. def __str__(self) -> str:
  33. return "<LocatorHandlerGreenlet>"
  34. class EventGreenlet(greenlet.greenlet):
  35. def __str__(self) -> str:
  36. return "<EventGreenlet>"