Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: inercia/evy
base: a6588b2fc7
...
head fork: inercia/evy
compare: 1bf86b1ce7
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Showing with 37 additions and 41 deletions.
  1. +37 −41 evy/uv/watchers.py
View
78 evy/uv/watchers.py
@@ -30,6 +30,7 @@
import sys
import signal
+from functools import partial
from evy.uv.interface import libuv, ffi, handle_is_active
@@ -37,24 +38,39 @@
class Watcher(object):
"""
- An general watcher
+ An abstract watcher
"""
+
libuv_start_this_watcher = None
libuv_stop_this_watcher = None
libuv_handle_type = 'uv_handle_t *'
- _callback = None
hub = None
- args = None
+
+ _callback = None
_flags = 0
+ _start_func = None
+ _stop_func = None
+
def __init__(self, _hub, ref = True):
+ assert self._uv_handle and self._cb
+
self.hub = _hub
if ref:
self._flags = 0
else:
self._flags = 4
+ ## prepare a function for starting the watcher
+ if self.libuv_start_this_watcher:
+ self._start_func = partial(self.libuv_start_this_watcher, self._uv_handle, self._cb)
+
+ ## .. and another one for stopping it
+ if self.libuv_stop_this_watcher:
+ assert self._uv_handle
+ self._stop_func = partial(self.libuv_stop_this_watcher, self._uv_handle)
+
def _run_callback(self, handle, *args):
"""
This is invoked as a callback when the watcher completes (for example, when a timer is expired)
@@ -65,7 +81,7 @@ def _run_callback(self, handle, *args):
if self.callback:
try:
- self.callback(*self.args)
+ self.callback()
except:
try:
self.hub.handle_error(self, *sys.exc_info())
@@ -122,37 +138,21 @@ def _set_ref(self, value):
ref = property(_get_ref, _set_ref)
-
- ##
- ## callbacks
- ##
-
- def _get_callback(self):
- return self._callback
-
- def _set_callback(self, callback):
- assert callable(callback)
- self._callback = callback
-
- callback = property(_get_callback, _set_callback)
-
##
## start/stop
##
- def start(self, callback, *args):
+ def start(self, callback, *args, **kwargs):
"""
Start the watcher
:param callback: callback to invoke when the watcher is done
:param args: arguments for calling the callback
"""
- self.callback = callback
- self.args = args
+ self.callback = partial(callback, *args, **kwargs)
self._libuv_unref()
- if self.libuv_start_this_watcher:
- self.libuv_start_this_watcher(self._uv_handle, self._cb)
+ if self._start_func: self._start_func()
self._python_incref()
@@ -164,11 +164,10 @@ def stop(self):
libuv.uv_ref(self.hub._uv_ptr)
self._flags &= ~2
- if self.libuv_stop_this_watcher:
- self.libuv_stop_this_watcher(self._uv_handle)
+ if self._stop_func: self._stop_func()
+
+ self.callback = None
- self._callback = None
- self.args = None
if self._flags & 1:
# Py_DECREF(<PyObjectPtr>self)
self._flags &= ~1
@@ -245,7 +244,7 @@ def __init__(self, hub, fd, events, ref = True):
libuv.uv_poll_init(hub._uv_ptr, self._uv_handle, fd)
Watcher.__init__(self, hub, ref = ref)
- def start(self, callback, *args, **kw):
+ def start(self, callback, *args, **kwargs):
"""
Start the file descriptor poller
@@ -254,8 +253,7 @@ def start(self, callback, *args, **kw):
:param kw: keywords arguments for calling the callback
:return: None
"""
- self.callback = callback
- self.args = args
+ self.callback = partial(callback, *args, **kwargs)
self._libuv_unref()
@@ -331,7 +329,7 @@ def __init__(self, hub, after = 0.0, repeat = 0.0, ref = True):
libuv.uv_timer_init(hub._uv_ptr, self._uv_handle)
Watcher.__init__(self, hub, ref = ref)
- def start(self, callback, *args, **kw):
+ def start(self, callback, *args, **kwargs):
"""
Start the timer
@@ -340,9 +338,8 @@ def start(self, callback, *args, **kw):
:param kw: keywords arguments for calling the callback
:return: None
"""
- update = kw.get("update", True)
- self.callback = callback
- self.args = args
+ update = kwargs.get("update", True)
+ self.callback = partial(callback, *args, **kwargs)
self._libuv_unref()
@@ -361,13 +358,13 @@ def at(self):
"""
return self._after
- def again(self, callback, *args, **kw):
+ def again(self, callback, *args, **kwargs):
"""
Stop the timer, and if it is repeating restart it using the repeat value as the timeout.
"""
- update = kw.get("update", True)
- self.callback = callback
- self.args = args
+ update = kwargs.get("update", True)
+ self.callback = partial(callback, *args, **kwargs)
+
self._libuv_unref()
if update:
libuv.uv_now_update(self.hub._uv_ptr)
@@ -431,7 +428,7 @@ def __init__(self, hub, signalnum, ref = True):
libuv.uv_signal_init(hub._uv_ptr, self._uv_handle)
Watcher.__init__(self, hub, ref = ref)
- def start(self, callback, *args, **kw):
+ def start(self, callback, *args, **kwargs):
"""
Start the signal watcher
@@ -440,8 +437,7 @@ def start(self, callback, *args, **kw):
:param kw: keywords arguments for calling the callback
:return: None
"""
- self.callback = callback
- self.args = args
+ self.callback = partial(callback, *args, **kwargs)
self._libuv_unref()

No commit comments for this range

Something went wrong with that request. Please try again.