From 56b3969982deac500fdabc458af50f107a373161 Mon Sep 17 00:00:00 2001 From: Tomasz Prus Date: Sat, 19 Aug 2017 01:45:13 +0200 Subject: [PATCH 1/2] feat: add examples for asynchronous usage (callback, asycio) --- examples/async.py | 31 +++++++++++++++++++++++++++++++ examples/callbacks.py | 20 ++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 examples/async.py create mode 100644 examples/callbacks.py diff --git a/examples/async.py b/examples/async.py new file mode 100644 index 0000000000..61ce9d7cd5 --- /dev/null +++ b/examples/async.py @@ -0,0 +1,31 @@ +import asyncio +import functools +from kubernetes import client, config + + +# return a callback and assigned future where data will be stored +def create_async_callback(): + def set_result(loop, future, data): + loop.call_soon_threadsafe(future.set_result, data) + future = asyncio.Future() + return functools.partial(set_result, asyncio.get_event_loop(), future), future + + +async def main(): + + # Configs can be set in Configuration class directly or using helper utility + config.load_kube_config() + + v1 = client.CoreV1Api() + print("Listing pods with their IPs:") + + callback, future = create_async_callback() + v1.list_pod_for_all_namespaces(watch=False, callback=callback) + ret = await future + for i in ret.items: + print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) + + +if __name__ == "__main__": + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) diff --git a/examples/callbacks.py b/examples/callbacks.py new file mode 100644 index 0000000000..5f914245bf --- /dev/null +++ b/examples/callbacks.py @@ -0,0 +1,20 @@ +import time +from kubernetes import client, config + + +def recv_namespace(data): + print("Listing pods with their IPs:") + for i in data.items: + print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) + + +# Configs can be set in Configuration class directly or using helper utility +config.load_kube_config() + +v1 = client.CoreV1Api() +thread = v1.list_pod_for_all_namespaces(watch=False, callback=recv_namespace) + +# do something in the main thread +for i in range(0, 10): + print('-- do something --') + time.sleep(0.5) From d457307a22849212673c79d05a0eb6e97e41a2b8 Mon Sep 17 00:00:00 2001 From: Tomasz Prus Date: Sat, 19 Aug 2017 02:21:05 +0200 Subject: [PATCH 2/2] fix: pep8 --- examples/async.py | 13 ++++++++++--- examples/callbacks.py | 7 ++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/examples/async.py b/examples/async.py index 61ce9d7cd5..26c38048ac 100644 --- a/examples/async.py +++ b/examples/async.py @@ -1,5 +1,6 @@ import asyncio import functools + from kubernetes import client, config @@ -8,12 +9,14 @@ def create_async_callback(): def set_result(loop, future, data): loop.call_soon_threadsafe(future.set_result, data) future = asyncio.Future() - return functools.partial(set_result, asyncio.get_event_loop(), future), future + return functools.partial( + set_result, asyncio.get_event_loop(), future), future async def main(): - # Configs can be set in Configuration class directly or using helper utility + # Configs can be set in Configuration class directly or using helper + # utility config.load_kube_config() v1 = client.CoreV1Api() @@ -23,7 +26,11 @@ async def main(): v1.list_pod_for_all_namespaces(watch=False, callback=callback) ret = await future for i in ret.items: - print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) + print( + "%s\t%s\t%s" % + (i.status.pod_ip, + i.metadata.namespace, + i.metadata.name)) if __name__ == "__main__": diff --git a/examples/callbacks.py b/examples/callbacks.py index 5f914245bf..193fe8358d 100644 --- a/examples/callbacks.py +++ b/examples/callbacks.py @@ -1,11 +1,16 @@ import time + from kubernetes import client, config def recv_namespace(data): print("Listing pods with their IPs:") for i in data.items: - print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) + print( + "%s\t%s\t%s" % + (i.status.pod_ip, + i.metadata.namespace, + i.metadata.name)) # Configs can be set in Configuration class directly or using helper utility