[PEAK] Task containment and isolation

Phillip J. Eby pje at telecommunity.com
Sat Jul 28 11:25:11 EDT 2007

At 01:36 PM 7/28/2007 +0300, Niki Spahiev wrote:
>2007/7/27, Phillip J. Eby <pje at telecommunity.com>:
> > At 04:51 PM 7/26/2007 -0400, Phillip J. Eby wrote:
> > >* ``task``, ``Pause``, ``Return``, ``resume()``, and ``TaskCell``
> > >   (support for pseudothreads similar to ``peak.events.Task``)
> > >...
> > >* Remove dependency on the unreleased "Contextual" package
> > >   (we're currently only using one exception class from there, and
> > >   the features that will require it in future will be in either the
> > >   "TrellisIO" or "TrellisDB" packages; more details below)
> >
> > It looks like this isn't quite as easy as I thought.  There's a
> > conflict, in that if I support pseudothreads, I need a way to let
> > them have different "Contextual" contexts, and prevent pollution
> > between different contexts.  Otherwise, there's no way to create
> > fully isolated pseudo-threads with their own database connections,
> > transactions, etc.
>How does this relate with stackless python and greenlets?

The "Contextual" library allows you to have an arbitrary number of 
logical threads, but you must explicitly switch context when 
execution moves from one logical thread to another within the same 
physical thread.

Assuming that I make the Trellis automatically do the context 
switching (which I plan to, after Contextual is ready for release), 
then Trellis rules and tasks will always run in the same context as 
they were created in, regardless of when/where/how they are called -- 
with or without greenlets or stackless.

(Note: I am assuming that stackless tasks and greenlets can only be 
run in the thread that created them; i.e., that you can't 
context-switch across threads.)

If you are using Contextual without the Trellis, and you want 
tasklets or greenlets to have their own "Contextual" context, you 
must explicitly use State.swap() operations to switch state when 
crossing logical task boundaries.  Your definition of "logical task 
boundaries" is up to you.  :)

(Note, by the way, that Contextual swaps an *entire* state in one 
operation; you don't need to say which services you are using or 
anything like that.  It simply swaps in or out your entire active set 
of services, settings, etc.  This was specifically designed so that 
generators, tasklets, and other general-purpose task wrappers could 
transparently change context in a single very fast operation.)

More information about the PEAK mailing list