diff --git a/examples/async.py b/examples/async.py new file mode 100644 index 000000000..26c38048a --- /dev/null +++ b/examples/async.py @@ -0,0 +1,38 @@ +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 000000000..193fe8358 --- /dev/null +++ b/examples/callbacks.py @@ -0,0 +1,25 @@ +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)