python
python copied to clipboard
Not possible to close watch stream immediately
Hello Kubernetes Python Community,
I would like to ask for a help about what is the intended way to close the watch stream immediately.
Consider my simple reproducible example python3 code where I want to close the stream by sending signal:
#!/usr/bin/env python3
import signal
from kubernetes import client, config, watch
watcher_cm = watch.Watch()
def cleanup(sig, frame):
print("Cleaning up watcher streams")
watcher_cm.stop()
print("Cleanup Finished")
def main():
config.load_kube_config()
signal.signal(signal.SIGINT, cleanup)
signal.signal(signal.SIGTERM, cleanup)
v1 = client.CoreV1Api()
event: client.CoreV1Event
try:
for event in watcher_cm.stream(
func=v1.list_namespaced_config_map,
namespace="kube-system",
label_selector="app=kube-proxy"):
print('Registered new ConfigMap event')
except Exception as e:
print(e)
main()
- I run the script;
- It registers one ConfigMap;
- Ten I hit Ctrl+C;
- I expect that the watch stream will try to end as soon as possible, but it is hanging. Probably it waits for one more
next()
event before it closes?; - While the script is hanging, in the second terminal I execute
kubectl label cm kube-proxy foo=bar
to create new event; - The script finishes now;
$ python3 test.py
Registered new ConfigMap event
^CCleaning up watcher streams
Cleanup Finished
# Here the script is hanging...
# But when I do `kubectl label cm kube-proxy foo=bar` in the second terminal, it registers one more event and closes the watch stream.
Registered new ConfigMap event
The question is how do I exit the watch stream without waiting for one more event?
Environment:
- Kubernetes version (
kubectl version
): v1.23.9 - OS (e.g., MacOS 10.13.6): Fedora 35
- Python version (
python --version
): 3.10.4 - Python client version (
pip list | grep kubernetes
): kubernetes 24.2.0
Thank you very much for any help.
I would like to contribute to solve this issue. So if possible, can you please assign this issue to me and help me with some resources for the issue? @dims @lavalamp @roycaihw @SEJeff @rawler @flavio @yuvipanda @brejoc @spiffxp @gliptak @iciclespider @larsks @jfrabaute @aschleck @idvoretskyi @AurelienGasser @tg123 @webwurst @hajowieland @mccullya @willthames @zapman449 @WilliamDenniss @unki @juliantaylor @dhague @justaugustus @mitar
Hi @sharmadhiraj86,
I cannot assign anyone as I am not maintainer of the project. Maybe @fabianvf or someone else from OWNERS file could help here?
But I'd say if you open a PR it will probably be automatically assigned to someone from owners.
Instead of tagging every single contributor (which is very annoying), just do the work and open a pull request please.
Instead of tagging every single contributor (which is very annoying), just do the work and open a pull request please.
But before that, I need this task assigned to me officially as I have to show this as a proof.
Anyone can contribute when they want. Please just send a PR.
Try coding your cleanup
method like this:
def cleanup(sig, frame):
print("Cleaning up watcher streams")
raise Exception('Exit the watcher event loop')
From socket.recv:
Changed in version 3.5: If the system call is interrupted and the signal handler does not raise an exception, the method now retries the system call instead of raising an InterruptedError exception (see PEP 475 for the rationale).
Hi @iciclespider,
Thank you for your advice and useful links. It works now :pray:!
From my side this solves the issue. If you would like, I can open PR with an example to better clarify how to interrupt the watch stream correctly. If not feel free to close the issue.