Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions encore/concurrent/futures/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#
# (C) Copyright 2014 Enthought, Inc., Austin, TX
# All right reserved.
#
# This file is open source software distributed according to the terms in
# LICENSE.txt
#

from functools import wraps


def dispatch(dispatcher=None, call=None):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd much prefer to see two separate decorators here. We might even drop the call option altogether: did you have specific use-cases in mind for this? If not, can we wait until those use-cases exist?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd much prefer to see two separate decorators here.

No problem. Now to name them...

We might even drop the call option altogether: did you have specific use-cases in mind for this?

I had traits.trait_notifiers.ui_dispatch in mind for call. I would be happy to drop it if we had a UIExecutor that dispatched to a UI thread. (That can get pretty hairy, though.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is meant to be sugar, then can't dispatcher handle either a string, or callable or real dispatcher instance? If you want this to be explicit, then it seems more consistent to have dispatcher=None, dispatcher_trait=None, callback=None.

""" Dispatch method calls using a dispatcher.

All calls made to the decorated method are submitted to a "dispatcher" (an
executor, work scheduler, or anything else with a "submit" method), or via
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth specifying that the submit method has to have the correct signature.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is in the parameters description, but it's worth being explicit.

some other call. The decorated function does not await any feedback from
the dispatcher. For example, futures returned by an executor are ignored.
The decorated method returns nothing.

Parameters
----------
dispatcher : dispatcher or str, optional
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the 'dispatcher' case useful? Would we lose much by only allowing the 'str' option?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'dispatcher' case is rather awkward, and probably not all that useful. Any case where it could be used would probably be better done with the 'str' option.

The object used to dispatch calls. A dispatcher is any object with a
"submit" method with the Executor.submit call signature. If this is a
string, it must identify an attribute on the instance to which the
method is bound.
call : callable or str, optional
A callable used to dispatch calls, or a string identifying
a callable on the instance to which the method is bound. The callable
must support the Executor.submit call signature.

Notes
-----
Exactly one of ``dispatcher`` or ``call`` must be specified.

"""
if (dispatcher, call).count(None) != 1:
msg = "Provide exactly one of 'dispatch' or 'call'"
raise ValueError(msg)

def decorate_with_dispatcher(func):

lookup_dispatcher = isinstance(dispatcher, basestring)

@wraps(func)
def wrapper(self, *args, **kw):

if lookup_dispatcher:
dispatcher_ = getattr(self, dispatcher)
else:
dispatcher_ = dispatcher

dispatcher_.submit(func, self, *args, **kw)

return wrapper

def decorate_with_call(func):

lookup_call = isinstance(call, basestring)

@wraps(func)
def wrapper(self, *args, **kw):

if lookup_call:
call_ = getattr(self, call)
else:
call_ = call

call_(func, self, *args, **kw)

return wrapper

return decorate_with_dispatcher if call is None else decorate_with_call
95 changes: 95 additions & 0 deletions encore/concurrent/futures/tests/test_decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#
# (C) Copyright 2014 Enthought, Inc., Austin, TX
# All right reserved.
#
# This file is open source software distributed according to the terms in
# LICENSE.txt
#
import unittest

from encore.concurrent.futures.decorators import dispatch


class TestDispatcher(object):

def __init__(self):
self.calls = []

def submit(self, func, *args, **kw):
# Drop 'self' from args
self.calls.append((args[1:], kw))
func(*args, **kw)

def __call__(self, func, *args, **kw):
# Drop 'self' from args
self.calls.append((args[1:], kw))
func(*args, **kw)


TEST_DISPATCHER = TestDispatcher()


class TestClass(object):

def __init__(self):
self.dispatcher = TestDispatcher()
self.calls = []

@dispatch(dispatcher=TEST_DISPATCHER)
def dispatcher_wrapped(self, *args, **kw):
self.calls.append((args, kw))

@dispatch(dispatcher="dispatcher")
def dispatcher_string_wrapped(self, *args, **kw):
self.calls.append((args, kw))

@dispatch(call=TEST_DISPATCHER)
def call_wrapped(self, *args, **kw):
self.calls.append((args, kw))

@dispatch(call="dispatcher")
def call_string_wrapped(self, *args, **kw):
self.calls.append((args, kw))


class TestDispatch(unittest.TestCase):

def setUp(self):
self.obj = TestClass()
self.calls = [
((1, 2), {"c": 3, "d": 4}),
((5, 6), {"e": 7, "f": 8})
]
TEST_DISPATCHER.calls = []

def _make_calls(self, bound_method):
for args, kw in self.calls:
bound_method(*args, **kw)

def test_dispatcher(self):
self._make_calls(self.obj.dispatcher_wrapped)
self.assertEqual(self.obj.calls, TEST_DISPATCHER.calls)
self.assertEqual(self.obj.calls, self.calls)

def test_dispatcher_string(self):
self._make_calls(self.obj.dispatcher_string_wrapped)
self.assertEqual(self.calls, self.obj.dispatcher.calls)
self.assertEqual(self.obj.calls, self.calls)

def test_call(self):
self._make_calls(self.obj.call_wrapped)
self.assertEqual(self.calls, TEST_DISPATCHER.calls)
self.assertEqual(self.obj.calls, self.calls)

def test_call_string(self):
self._make_calls(self.obj.call_string_wrapped)
self.assertEqual(self.calls, self.obj.dispatcher.calls)
self.assertEqual(self.obj.calls, self.calls)

def test_wrong_args(self):
self.assertRaises(ValueError, dispatch)
self.assertRaises(ValueError, dispatch, 'foo', 'bar')


if __name__ == '__main__':
unittest.main()