Skip to content
This repository has been archived by the owner on Mar 13, 2022. It is now read-only.

Commit

Permalink
Add flag to enable keep the watch action working all the time
Browse files Browse the repository at this point in the history
  • Loading branch information
lichen2013 committed Oct 17, 2017
1 parent a41c447 commit 48223bf
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions watch/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ def unmarshal_event(self, data, return_type):
js['object'] = self._api_client.deserialize(obj, return_type)
return js

def stream(self, func, *args, **kwargs):
def stream(self, func, keep=False, *args, **kwargs):
"""Watch an API resource and stream the result back via a generator.
:param func: The API function pointer. Any parameter to the function
can be passed after this parameter.
:param keep: Flag to keep the watch work all the time.
:return: Event object with these keys:
'type': The type of event such as "ADDED", "DELETED", etc.
'raw_object': a dict representing the watched object.
Expand All @@ -113,12 +115,17 @@ def stream(self, func, *args, **kwargs):
return_type = self.get_return_type(func)
kwargs['watch'] = True
kwargs['_preload_content'] = False
resp = func(*args, **kwargs)
try:
for line in iter_resp_lines(resp):
yield self.unmarshal_event(line, return_type)
if self._stop:
break
finally:
resp.close()
resp.release_conn()

while True:
resp = func(*args, **kwargs)
try:
for line in iter_resp_lines(resp):
yield self.unmarshal_event(line, return_type)
if self._stop:
break
finally:
resp.close()
resp.release_conn()

if not keep or self._stop:
break

0 comments on commit 48223bf

Please sign in to comment.