3

How can i ask to docker-py events to stop events stream iterable ? I want, for exemple, simply stop to watch dockers events.

from docker import Client


client = Client(base_url='unix://var/run/docker.sock')
events = client.events(decode=True)

for event in events:
    print(event)

print('exited')

client.events() return generator build here.

2 Answers 2

2

The client.events() generator is blocking on a unix socket recv call. If you can't wait until the next event and break out of the loop then, you'll need to interrupt the socket.

Unfortunately I couldn't find a clean way to do this via docker-py. The only way I found requires using/overriding private methods which of course is fragile. If you're looking for a robust solution I would recommend making the api call directly and then you're free to set your own timeouts on the socket or use it in non-blocking mode.

With that being said, to directly answer the question here's a way to patch the Client class to add the api response as a property to the generator returned by events().

from docker import Client

class CustomGenerator(object):
    def __init__(self, stream, response, decode):
        self.stream = stream
        self.response = response
        self.decode = decode

    def __iter__(self):
        for item in super(CustomClient, self.stream).\
                _stream_helper(self.response, self.decode):
            yield item

class CustomClient(Client):
    def _stream_helper(self, response, decode=False):
        return CustomGenerator(self, response, decode)

Once you have the response you can shutdown the socket which will raise an exception in the generator. In this example I use a thread to stop listening after 5 seconds.

from threading import Timer
import requests
import socket

client = CustomClient(base_url="unix://var/run/docker.sock")
events = client.events()

def listen_for_events():
    print("listening")

    try:
        for event in events:
            print(event)
    except requests.packages.urllib3.exceptions.ProtocolError:
        pass

    print("done listening")

def stop_listening():
    sock = client._get_raw_response_socket(events.response)
    sock.shutdown(socket.SHUT_RDWR)

Timer(5, stop_listening).start()

listen_for_events()
0

To be able to interrupt wait for the next event you can employ since and until arguments of events() method by listening events in series of two second timeframes.

from datetime import datetime, timedelta

import docker
client = docker.from_env()

delta = timedelta(seconds=2)
since = datetime.utcnow()
until = datetime.utcnow() + delta
while True:
    for event in client.events(since=since, until=until, decode=True):
        print(event)
    since = until
    until = datetime.utcnow() + delta

Since you wait for events for 2 seconds, you execution thread will be blocked for maximum 2 seconds. So, if user hits Ctrl+C, the script will terminate shortly.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.