btubbs/sseclient

How to close the sseclient?

ctranxuan opened this issue · 2 comments

Hi,
That's more a question than an issue: how do we close the sseclient? I mean how do we close the connection?
Sorry, it might be a python newbie question...

toabi commented

I was thinking about this, but maybe the best approach is to just pick out single events by using next(…) on it until you are satisfied.

Sorry I missed this issue when it was filed back in September.

@toabi's suggestion should work. If you don't want to block indefinitely you can call .next() repeatedly in your own loop, and stop when you don't want to keep going.

You could also block and use break to get out of the for loop when blocking, as in the for msg in messages pattern used in the README.

I haven't tested it, but I expect that just letting your SSEClient object go out of scope would mean that its connection gets cleaned up by garbage collection. If you want to explicitly close the connection, the Response object from the Requests library is available as the .resp attribute on the SSEClient, and it has a .close() method. See http://docs.python-requests.org/en/master/api/#requests.Response.