Ability to stop docker-py.events() generator from another thread?
See original GitHub issueWhen we want to listen for live docker events with docker-py we have 2 options:
-
Fetch events with time limits like
since
anduntil
. Ifuntil
is the future date, code is blocking until we meet that date (but not pass over). Minimal time resolution forsince
anduntil
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:
- Created 7 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
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.
@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:
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?