question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to stop docker-py.events() generator from another thread?

See original GitHub issue

When we want to listen for live docker events with docker-py we have 2 options:

  • Fetch events with time limits like since and until. If until is the future date, code is blocking until we meet that date (but not pass over). Minimal time resolution for since and until is one second.

  • Fetch events live without time limits. Generator is blocking until some event pops out.

In projects based on for eg. Python Twisted, we have single event loop thread. We can’t run blocking queries, because we don’t want to block whole application. That’s why we execute blocking code in threads. Each 2 options I mentioned above we have problems.

  • Fetching events without time limits can’t be stopped. And when whole application is closing, thread is still running, so Twisted waits forever for ~thread.join. We have 0 control over generator, because it uses internal waits for some events. We can’t use a variable to tell thread that is should be still running. We are totally stuck.

  • We are forced to use fetching with time limits. We want to get events live, so we make smallest possible time chunks. Even smallest chunks gives us up to 1 second delay, so it’s not live. I don’t even mention about logic to manage that chunked fetching.

Two scenarios are bad.

Is there something that I’m not aware of, or we really can’'t do much more?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
TomasTomecekcommented, Dec 22, 2016

Would be nice if this was part of docker-py – ability to shut down the generator.

I agree that you have to wait for next event to pop up. I don’t mind that in my application and certainly understand when this is not acceptable in your application.

1reaction
approxitcommented, Dec 21, 2016

@TomasTomecek Thanks for response.

Yes, threading.Event WOULD do the job, but browsing your code I think you’re getting to point I wrote about:

Fetch events live without time limits. Generator is blocking until some event pops out.

So yes, you are using theading.Event, but it will stop that thread as soon as new docker.event arrives and you will return to your code that can check threading.Event. Not earlier.

Browsing even more I’ve finally found this stackoverflow question which answer would work. A little dirty hack… @shin- any chances to make it a little more official?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stop docker-py events stream iteration - Stack Overflow
The client.events() generator is blocking on a unix socket recv call. ... In this example I use a thread to stop listening after...
Read more >
docker kill - Docker Documentation
The docker kill subcommand kills one or more containers. The main process inside the container is sent SIGKILL signal (default), or the signal...
Read more >
docker events - Docker Documentation
To exit the docker events command, use CTRL+C . Filter events by time . You can filter the output by an absolute...
Read more >
docker stats - Docker Documentation
The docker stats command returns a live data stream for running containers. To limit data to one or more specific containers, specify a...
Read more >
Runtime metrics | Docker Documentation
Linux Containers rely on control groups which not only track groups of processes, but also expose metrics about CPU, memory, and block I/O...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found