Skip to content

Commit bed4ae1

Browse files
committed
Accept both coroutines and coroutine functions
1 parent 7865347 commit bed4ae1

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

examples/leaderelection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ async def example_end_func():
6565
lease_duration=17,
6666
renew_deadline=15,
6767
retry_period=5,
68-
onstarted_leading=example_start_func(),
69-
onstopped_leading=example_end_func(),
68+
# Coroutines are also accepted, to facilitate providing context
69+
# (e.g. passing apic)
70+
onstarted_leading=example_start_func,
71+
onstopped_leading=example_end_func,
7072
)
7173

7274
# Enter leader election

kubernetes_asyncio/leaderelection/electionconfig.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,27 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from collections.abc import Coroutine # noqa:F401
15+
from collections.abc import Callable, Coroutine # noqa:F401
1616

1717

1818
class Config:
1919
# Validate config, exit if an error is detected
2020

21-
# onstarted_leading and onstopped_leading are defined as coroutines rather
22-
# than callables in order to faciliate passing context. For example, this
23-
# allows the ApiClient used by the leader election to be shared and reused.
21+
# onstarted_leading and onstopped_leading accept either coroutines or
22+
# coroutine functions. Coroutines faciliate passing context, but coroutine
23+
# functions can be simpler when passing context is not required.
24+
#
25+
# One example of when passing context is helpful is sharing the ApiClient
26+
# used by the leader election, which can then be used for subsequent
27+
# Kubernetes API operations upon onstopped_leading or onstopped_leading.
2428
def __init__(
2529
self,
2630
lock,
2731
lease_duration,
2832
renew_deadline,
2933
retry_period,
30-
onstarted_leading, # type: Coroutine
31-
onstopped_leading=None, # type: Coroutine | None
34+
onstarted_leading, # type: Coroutine | Callable[[], Coroutine]
35+
onstopped_leading=None, # type: Coroutine | Callable[[], Coroutine] | None
3236
):
3337
self.jitter_factor = 1.2
3438

kubernetes_asyncio/leaderelection/leaderelection.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import asyncio
1616
import datetime
17+
import inspect
1718
import json
1819
import logging
1920
import sys
@@ -55,7 +56,13 @@ async def run(self):
5556
"%s successfully acquired lease", self.election_config.lock.identity
5657
)
5758

58-
task = asyncio.create_task(self.election_config.onstarted_leading)
59+
onstarted_leading_coroutine = (
60+
self.election_config.onstarted_leading
61+
if inspect.iscoroutine(self.election_config.onstarted_leading)
62+
else self.election_config.onstarted_leading()
63+
)
64+
65+
task = asyncio.create_task(onstarted_leading_coroutine)
5966

6067
await self.renew_loop()
6168

@@ -68,7 +75,11 @@ async def run(self):
6875
# preserved in order to continue to provide an interface similar to
6976
# the one provided by `kubernetes-client/python`.
7077
if self.election_config.onstopped_leading is not None:
71-
await self.election_config.onstopped_leading
78+
await (
79+
self.election_config.onstopped_leading
80+
if inspect.iscoroutine(self.election_config.onstopped_leading)
81+
else self.election_config.onstopped_leading()
82+
)
7283

7384
async def acquire(self):
7485
# Follower

0 commit comments

Comments
 (0)