|  | 
|  | 1 | +(engines-topic)= | 
|  | 2 | + | 
|  | 3 | +# Command Engines | 
|  | 4 | + | 
|  | 5 | +libtmux can execute tmux commands through different "engines"—pluggable | 
|  | 6 | +backends that share the same high-level API but vary in transport mechanics. | 
|  | 7 | +This abstraction keeps the Python surface area stable while allowing you to | 
|  | 8 | +choose the best option for your environment. | 
|  | 9 | + | 
|  | 10 | +## Available engines | 
|  | 11 | + | 
|  | 12 | +``SubprocessEngine`` (default) | 
|  | 13 | +: Launches a short-lived ``tmux`` process for each command, matching the | 
|  | 14 | +  behaviour libtmux shipped with historically. This is the most broadly | 
|  | 15 | +  compatible approach and requires no extra configuration. | 
|  | 16 | + | 
|  | 17 | +``ControlModeEngine`` | 
|  | 18 | +: Starts ``tmux`` with the ``-C`` flag and communicates using tmux’s control | 
|  | 19 | +  mode protocol. Because it keeps a persistent control connection open, it can | 
|  | 20 | +  be more efficient for workflows that issue many commands in rapid succession | 
|  | 21 | +  and is a stepping stone toward richer, event-driven integrations. | 
|  | 22 | + | 
|  | 23 | +Both engines expose the same :class:`~libtmux.engines.base.CommandResult` | 
|  | 24 | +contract, so all existing consumers (e.g., :class:`~libtmux.Server`, | 
|  | 25 | +:class:`~libtmux.Session`, :class:`~libtmux.Window`, :class:`~libtmux.Pane`) | 
|  | 26 | +continue to operate identically regardless of which engine you choose. | 
|  | 27 | + | 
|  | 28 | +## Choosing an engine | 
|  | 29 | + | 
|  | 30 | +```python | 
|  | 31 | +>>> import libtmux | 
|  | 32 | +>>> from libtmux.engines import ControlModeEngine | 
|  | 33 | +>>> server = libtmux.Server(engine=ControlModeEngine()) | 
|  | 34 | +``` | 
|  | 35 | + | 
|  | 36 | +If you omit the ``engine`` argument, libtmux defaults to | 
|  | 37 | +:class:`~libtmux.engines.subprocess.SubprocessEngine` for full backward | 
|  | 38 | +compatibility. You can inject the engine at construction time—fixtures in the | 
|  | 39 | +test suite demonstrate this pattern—and all child objects created from the | 
|  | 40 | +server inherit the same backend automatically. | 
|  | 41 | + | 
|  | 42 | +## Compatibility notes | 
|  | 43 | + | 
|  | 44 | +- Control mode is available in tmux 2.1 and newer. Ensure your tmux binary | 
|  | 45 | +  supports ``-C`` before opting in. | 
|  | 46 | +- When using control mode, libtmux opens a dedicated tmux process per engine | 
|  | 47 | +  instance. Create one :class:`~libtmux.Server` per desired control connection | 
|  | 48 | +  to avoid unnecessary processes. | 
|  | 49 | +- Both engines honour ``socket_name``, ``socket_path`` and ``config_file`` | 
|  | 50 | +  options just like the CLI would; the abstraction only swaps out how commands | 
|  | 51 | +  are forwarded to tmux. | 
|  | 52 | + | 
|  | 53 | +For deeper architectural background, explore the {ref}`traversal` topic to see | 
|  | 54 | +how commands flow through libtmux’s object model. | 
0 commit comments