@@ -61,56 +61,3 @@ asyncio.run(run_all_hosts())
6161` AsyncHostContext ` scopes the helper to a single host (specified by name or by a
6262` Host ` instance) but otherwise behaves the same. Both helpers accept a
6363` hosts= ` override when you need to target a subset of hosts for a specific call.
64-
65- ## SyncContext & SyncHostContext
66-
67- For synchronous code, ` pyinfra.sync_context.SyncContext ` and
68- ` pyinfra.sync_context.SyncHostContext ` mirror the async API without any asyncio
69- wrapping. The synchronous helpers still manage host connections for you and make
70- host facts available via ` host.get_fact(...) ` .
71-
72- ``` python
73- from pyinfra.api import Config, State, deploy
74- from pyinfra.facts.server import Hostname
75- from pyinfra.context import host
76- from pyinfra.operations import server
77- from pyinfra.sync_context import SyncContext, SyncHostContext
78-
79-
80- def run_all_hosts ():
81- inventory = ...
82- state = State(inventory, Config())
83-
84- @deploy (" Example sync deploy" )
85- def my_sync_deploy ():
86- server.shell(name = " Sync deploy op" , commands = " echo sync deploy" )
87-
88- with SyncContext(state):
89- server.shell(" echo hello from sync" )
90- hostnames = {
91- host: host.get_fact(Hostname)
92- for host in state.inventory
93- }
94- print (f " Hostnames: { hostnames} " )
95-
96- my_sync_deploy()
97-
98-
99- def run_single_host (state , host_name ):
100- @deploy (" Sync per-host deploy" )
101- def my_sync_host_deploy ():
102- server.shell(name = " Sync host deploy op" , commands = " echo sync host deploy" )
103-
104- with SyncHostContext(state, host_name):
105- assert host is not None
106- server.shell(" echo from sync single host" )
107- hostname = host.get_fact(Hostname)
108- print (hostname)
109-
110- my_sync_host_deploy()
111- ```
112-
113- ` SyncHostContext ` accepts either the host name or the ` Host ` object directly and
114- ensures only that host is connected for the duration of the context. The sync
115- and async helpers can be mixed within the same project—choose the one that fits
116- the surrounding code.
0 commit comments