[PEAK] trellis activity does not save/restore Contextual context between task switches

Sergey Schetinin maluke at gmail.com
Thu Apr 2 22:30:39 EDT 2009

Well, I use my own context manager (derived from Contextual but quite
different) so I could not expect to have it supported by Trellis
itself, yet I also though that I might need tasks that change the
state, so my solution goes like this:

class TaskGeneratorWrapper(object):
    def __init__(self, iterator):
        self.iterator = iterator
        map(self._add_wrap_call, ['next', 'send', 'throw'])
    def _add_wrap_call(self, name):
        if hasattr(self.iterator, name):
            func = getattr(self.iterator, name)
            wrapped = lambda *args: self.call_wrap(func, *args)
            setattr(self, name, wrapped)

class TaskStateSaver(TaskGeneratorWrapper):
    def __init__(self, iterator, stack=None):
        TaskGeneratorWrapper.__init__(self, iterator)
        self.stack = stack or [State.get()]
    def call_wrap(self, func, *args):
        prev = State.swap_stack(self.stack)
            r = func(*args)
            self.stack = State.swap_stack(prev)
        if hasattr(r, 'next'):
            return self.__class__(r, self.stack)
        return r

def stateful_task(method):
    return task()(lambda self: TaskStateSaver(method(self)))

Then use @stateful_task in place of @task

I'm not sure what calls you need to make to save/restore Contextual
state (State.swap() IIRC), so this change is up to you. And please
note that I did not fully test this yet. Please report on what changes
you had to make to adapt it to Contextual and if any issues surfaced.

- Sergey

On Fri, Apr 3, 2009 at 04:03, Andrew Svetlov <andrew.svetlov at gmail.com> wrote:
> Looks like my message was not delivered.
> I replay it.
> ----------------------------------------------------------
> Sorry for delay.
> I found an issue: it's trivial to add patch to activity.py but not
> easy to add unittest.
> Main problem is: existing codebase requires python 2.3 compatibility,
> but context.with_ works quite different than with statement in case of
> yielding result from generator. with_ retores context in finally block
> when generator yield statement reached and execution point goes out of
> with_ controlled function but _with_ statement doesn't do it until end
> of block reached.
> I cannot convert my unittests line-by-line.
> >From my perspective there are 2 solutions:
>  1. run uniittests for parallel task execution with context switching
> only if python version >= 2.5
>  2. make simple and not obvious test for saving/restoring
> context.State in task with direct modification of context state inside
> task generator and do nothing else.
> What version are you prefer?
> ------------------------------------------------------------------
> Guys, I really need suggestion.
> Patch is done, I included it in my monkeypatch list and for me all works.
> But I like to see this behavior in standard trellis.
> Problem is in unittests only.
> _______________________________________________
> PEAK mailing list
> PEAK at eby-sarna.com
> http://www.eby-sarna.com/mailman/listinfo/peak

Best Regards,
Sergey Schetinin

http://s3bk.com/ -- S3 Backup
http://word-to-html.com/ -- Word to HTML Converter

More information about the PEAK mailing list