Skip to content

Commit bd02b67

Browse files
Timmy WelchFizzadar
authored andcommitted
pyinfra.api.connect: Parallelize disconnecting from hosts
1 parent d412df0 commit bd02b67

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

pyinfra/api/connect.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,22 @@ def connect_all(state: "State"):
4646

4747

4848
def disconnect_all(state: "State"):
49-
for host in state.activated_hosts: # only hosts we connected to please!
50-
host.disconnect() # normally a noop
49+
"""
50+
Disconnect from all of the configured servers in parallel. Reads/writes state.inventory.
51+
52+
Args:
53+
state (``pyinfra.api.State`` obj): the state containing an inventory to connect to
54+
"""
55+
greenlet_to_host = {
56+
state.pool.spawn(host.disconnect): host
57+
for host in state.activated_hosts # only hosts we connected to please!
58+
}
59+
60+
with progress_spinner(greenlet_to_host.values()) as progress:
61+
for greenlet in gevent.iwait(greenlet_to_host.keys()):
62+
host = greenlet_to_host[greenlet]
63+
progress(host)
64+
65+
for greenlet, host in greenlet_to_host.items():
66+
# Raise any unexpected exception
67+
greenlet.get()

0 commit comments

Comments
 (0)