Debian Codegraph @

docs/topics/context_managers.md

raw symbols SCIP
1 (context_managers)=
2
3 # Context managers
4
5 When you create tmux objects through libtmux, they normally live until you
6 explicitly kill them. A context manager hands that cleanup back to Python: you
7 scope an object to a block, and libtmux kills the underlying tmux object the
8 moment you leave it — whether you exit cleanly or an exception unwinds the
9 stack. The {class}`~libtmux.Server`, {class}`~libtmux.Session`,
10 {class}`~libtmux.Window`, and {class}`~libtmux.Pane` classes (all main tmux
11 objects) support this.
12
13 Most readers never reach for this. If you're building a long-running
14 application, you typically let objects persist and tear them down yourself. The
15 context-manager form earns its keep in test fixtures and short-lived scripts,
16 where you want a tmux object to exist for exactly one block and then vanish.
17
18 Open two terminals:
19
20 Terminal one: start tmux in a separate terminal:
21
22 ```console
23 $ tmux
24 ```
25
26 Terminal two, `python` or `ptpython` if you have it:
27
28 ```console
29 $ python
30 ```
31
32 Import `libtmux`:
33
34 ```python
35 >>> import libtmux
36 ```
37
38 ## Server context manager
39
40 You create a temporary server that will be killed when you're done:
41
42 ```python
43 >>> with Server() as server:
44 ... session = server.new_session()
45 ... print(server.is_alive())
46 True
47 >>> print(server.is_alive()) # Server is killed after exiting context
48 False
49 ```
50
51 ## Session context manager
52
53 You create a temporary session that will be killed when you're done:
54
55 ```python
56 >>> server = Server()
57 >>> with server.new_session() as session:
58 ... print(session in server.sessions)
59 ... window = session.new_window()
60 True
61 >>> print(session in server.sessions) # Session is killed after exiting context
62 False
63 ```
64
65 ## Window context manager
66
67 You create a temporary window that will be killed when you're done:
68
69 ```python
70 >>> server = Server()
71 >>> session = server.new_session()
72 >>> with session.new_window() as window:
73 ... print(window in session.windows)
74 ... pane = window.split()
75 True
76 >>> print(window in session.windows) # Window is killed after exiting context
77 False
78 ```
79
80 ## Pane context manager
81
82 You create a temporary pane that will be killed when you're done:
83
84 ```python
85 >>> server = Server()
86 >>> session = server.new_session()
87 >>> window = session.new_window()
88 >>> with window.split() as pane:
89 ... print(pane in window.panes)
90 ... pane.send_keys('echo "Hello"')
91 True
92 >>> print(pane in window.panes) # Pane is killed after exiting context
93 False
94 ```
95
96 ## Nested context managers
97
98 For complex setups, you can nest contexts to build a whole tmux hierarchy at
99 once and have every layer torn down for you:
100
101 ```python
102 >>> with Server() as server:
103 ... with server.new_session() as session:
104 ... with session.new_window() as window:
105 ... with window.split() as pane:
106 ... pane.send_keys('echo "Hello"')
107 ... # Do work with the pane
108 ... # Everything is cleaned up automatically when exiting contexts
109 ```
110
111 This ensures that:
112
113 1. The pane is killed when exiting its context
114 2. The window is killed when exiting its context
115 3. The session is killed when exiting its context
116 4. The server is killed when exiting its context
117
118 The cleanup happens in reverse order (pane → window → session → server), ensuring proper resource management.
119
120 ## Benefits
121
122 Reaching for a context manager buys you a few things. Resources clean themselves
123 up the moment you leave the block, so you never manually call the
124 {meth}`~libtmux.Server.kill`, {meth}`~libtmux.Session.kill`,
125 {meth}`~libtmux.Window.kill`, or {meth}`~libtmux.Pane.kill` methods and the code
126 stays uncluttered. Because cleanup runs on the way out of the block, it fires
127 even when an exception unwinds the stack — so you don't leak a stray session or
128 pane on the error path. And when you nest contexts, the objects tear down in
129 hierarchical order, which keeps tmux's own bookkeeping consistent.
130
131 ## When to use
132
133 Use context managers when you're writing test fixtures, running short-lived
134 sessions, or managing several tmux servers that each need to disappear cleanly.
135 They also pay off in any script that might raise partway through, or when you're
136 spinning up an isolated environment that has to be cleaned up afterward.
137
138 [target]: http://man.openbsd.org/OpenBSD-5.9/man1/tmux.1#COMMANDS