subshell.py 1023 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """A thread for a subshell."""
  2. import asyncio
  3. from typing import Any
  4. import zmq
  5. from .socket_pair import SocketPair
  6. from .thread import BaseThread
  7. class SubshellThread(BaseThread):
  8. """A thread for a subshell.
  9. .. versionadded:: 7
  10. """
  11. def __init__(
  12. self,
  13. subshell_id: str,
  14. context: zmq.Context[Any],
  15. **kwargs,
  16. ):
  17. """Initialize the thread."""
  18. super().__init__(name=f"subshell-{subshell_id}", **kwargs)
  19. self.shell_channel_to_subshell = SocketPair(context, subshell_id)
  20. self.subshell_to_shell_channel = SocketPair(context, subshell_id + "-reverse")
  21. # When aborting flag is set, execute_request messages to this subshell will be aborted.
  22. self.aborting = False
  23. self.asyncio_lock = asyncio.Lock()
  24. def run(self) -> None:
  25. """Run the thread."""
  26. try:
  27. super().run()
  28. finally:
  29. self.shell_channel_to_subshell.close()
  30. self.subshell_to_shell_channel.close()