Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Use partial functions for starting/stopping watchers

  • Loading branch information...
commit 1bf86b1ce77c001cdf7e777bbfcb46a5c8b52062 1 parent e11116e
Alvaro authored

Showing 1 changed file with 20 additions and 6 deletions. Show diff stats Hide diff stats

  1. +20 6 evy/uv/watchers.py
26 evy/uv/watchers.py
@@ -38,23 +38,39 @@
38 38
39 39 class Watcher(object):
40 40 """
41   - An general watcher
  41 + An abstract watcher
42 42 """
  43 +
43 44 libuv_start_this_watcher = None
44 45 libuv_stop_this_watcher = None
45 46 libuv_handle_type = 'uv_handle_t *'
46 47
47   - _callback = None
48 48 hub = None
  49 +
  50 + _callback = None
49 51 _flags = 0
  52 + _start_func = None
  53 + _stop_func = None
  54 +
50 55
51 56 def __init__(self, _hub, ref = True):
  57 + assert self._uv_handle and self._cb
  58 +
52 59 self.hub = _hub
53 60 if ref:
54 61 self._flags = 0
55 62 else:
56 63 self._flags = 4
57 64
  65 + ## prepare a function for starting the watcher
  66 + if self.libuv_start_this_watcher:
  67 + self._start_func = partial(self.libuv_start_this_watcher, self._uv_handle, self._cb)
  68 +
  69 + ## .. and another one for stopping it
  70 + if self.libuv_stop_this_watcher:
  71 + assert self._uv_handle
  72 + self._stop_func = partial(self.libuv_stop_this_watcher, self._uv_handle)
  73 +
58 74 def _run_callback(self, handle, *args):
59 75 """
60 76 This is invoked as a callback when the watcher completes (for example, when a timer is expired)
@@ -136,8 +152,7 @@ def start(self, callback, *args, **kwargs):
136 152 self.callback = partial(callback, *args, **kwargs)
137 153 self._libuv_unref()
138 154
139   - if self.libuv_start_this_watcher:
140   - self.libuv_start_this_watcher(self._uv_handle, self._cb)
  155 + if self._start_func: self._start_func()
141 156
142 157 self._python_incref()
143 158
@@ -149,8 +164,7 @@ def stop(self):
149 164 libuv.uv_ref(self.hub._uv_ptr)
150 165 self._flags &= ~2
151 166
152   - if self.libuv_stop_this_watcher:
153   - self.libuv_stop_this_watcher(self._uv_handle)
  167 + if self._stop_func: self._stop_func()
154 168
155 169 self.callback = None
156 170

0 comments on commit 1bf86b1

Please sign in to comment.
Something went wrong with that request. Please try again.