(context_managers)=
libtmux provides context managers for all main tmux objects to ensure proper cleanup of resources. This is done through Python's with
statement, which automatically handles cleanup when you're done with the tmux objects.
Open two terminals:
Terminal one: start tmux in a separate terminal:
$ tmux
Terminal two, python
or ptpython
if you have it:
$ python
Import libtmux
:
import libtmux
Create a temporary server that will be killed when you're done:
>>> with Server() as server:
... session = server.new_session()
... print(server.is_alive())
True
>>> print(server.is_alive()) # Server is killed after exiting context
False
Create a temporary session that will be killed when you're done:
>>> server = Server()
>>> with server.new_session() as session:
... print(session in server.sessions)
... window = session.new_window()
True
>>> print(session in server.sessions) # Session is killed after exiting context
False
Create a temporary window that will be killed when you're done:
>>> server = Server()
>>> session = server.new_session()
>>> with session.new_window() as window:
... print(window in session.windows)
... pane = window.split()
True
>>> print(window in session.windows) # Window is killed after exiting context
False
Create a temporary pane that will be killed when you're done:
>>> server = Server()
>>> session = server.new_session()
>>> window = session.new_window()
>>> with window.split() as pane:
... print(pane in window.panes)
... pane.send_keys('echo "Hello"')
True
>>> print(pane in window.panes) # Pane is killed after exiting context
False
Context managers can be nested to create a clean hierarchy of tmux objects that are automatically cleaned up:
>>> with Server() as server:
... with server.new_session() as session:
... with session.new_window() as window:
... with window.split() as pane:
... pane.send_keys('echo "Hello"')
... # Do work with the pane
... # Everything is cleaned up automatically when exiting contexts
This ensures that:
- The pane is killed when exiting its context
- The window is killed when exiting its context
- The session is killed when exiting its context
- The server is killed when exiting its context
The cleanup happens in reverse order (pane → window → session → server), ensuring proper resource management.
Using context managers provides several advantages:
- Automatic Cleanup: Resources are automatically cleaned up when you're done with them
- Clean Code: No need to manually call
kill()
methods - Exception Safety: Resources are cleaned up even if an exception occurs
- Hierarchical Cleanup: Nested contexts ensure proper cleanup order
- Resource Management: Prevents resource leaks by ensuring tmux objects are properly destroyed
Context managers are particularly useful when:
- Creating temporary tmux objects for testing
- Running short-lived tmux sessions
- Managing multiple tmux servers
- Ensuring cleanup in scripts that may raise exceptions
- Creating isolated environments that need to be cleaned up afterward