cothread.cothread
index
/dls_sw/prod/common/python/cothread/1-14/cothread/cothread.py

Simple cooperative threading using coroutines.  The following functions
define the interface provided by this module.
 
    Spawn(function, arguments...)
        A new cooperative thread, or "task", is created as a call to 
        function(arguments).  Control is not transferred to the task until
        control is yielded.
 
    Sleep(delay)
    SleepUntil(time)
        The calling task is suspended until the given time.  Sleep(delay)
        suspends the task for at least delay seconds, SleepUntil(time)
        suspends until the specified time has passed (time is defined as the
        value returned by time.time()).
            Control is not returned to the calling task until all other
        active tasks have been processed.
        
    Yield()
        Yield() suspends control so that all other potentially busy tasks can
        run.  
 
Instances of the Event object can be used for communication between tasks.
The following Event object methods are relevant.
 
    Wait()
    Wait(timeout)
        Waits for the event object to be signalled or for the timeout to
        expire (if specified).  Returns True if a signal was received, False
        if a timeout ocurred.
 
    Signal()
        Signals the event object, releasing at least one waiting task.
 
Similarly the EventQueue can be used for communication.

 
Modules
       
bisect
collections
cothread.coselect
greenlet
os
sys
thread
time
traceback

 
Classes
       
__builtin__.object
ThreadedEventQueue
Timer
EventBase(__builtin__.object)
Event
EventQueue
Spawn
exceptions.Exception
Timedout

 
class Event(EventBase)
    Any number of tasks can wait for an event to occur.  A single value
can also be associated with the event.
 
 
Method resolution order:
Event
EventBase
__builtin__.object

Methods defined here:
AbortWait(self)
Called instead of performing a proper wait to release any resources
that might be consumed until the wait occurs.
Reset(self)
Resets the event (and erases the value).
Signal(self, value=None)
Signals the event.  Any waiting tasks are scheduled to be woken.
SignalException(self, exception)
Signals the event with an exception: the next call to wait will
receive an exception instead of a normal return value.
Wait(self, timeout=None)
The caller will block until the event becomes true, or until the
timeout occurs if a timeout is specified.  A Timeout exception is
raised if a timeout occurs.
__init__(self, auto_reset=True)
An event object is either signalled or reset.  Any task can wait
for the object to become signalled, and it will be suspended until
this occurs.  
 
The intial value can be specified, as can the behaviour on succesfully
signalling a process: if auto_reset=True is specified then only one
task at a time sees any individual signal on this object.
__nonzero__(self)
Tests whether the event is signalled.

Properties defined here:
value
get lambda self

Data and other attributes inherited from EventBase:
__dict__ = <dictproxy object>
dictionary for instance variables (if defined)
__weakref__ = <attribute '__weakref__' of 'EventBase' objects>
list of weak references to the object (if defined)

 
class EventQueue(EventBase)
    A queue of objects.  A queue can also be treated as an iterator.
 
 
Method resolution order:
EventQueue
EventBase
__builtin__.object

Methods defined here:
AbortWait(self)
Called instead of performing a proper wait to release any resources
that might be consumed until the wait occurs.
Signal(self, value)
Adds the given value to the tail of the queue.
Wait(self, timeout=None)
Returns the next object from the queue, or raises a Timeout
exception if the timeout expires first.
__init__(self)
__iter__(self)
An event queue can itself be treated as an iterator: this allows
event dispatching using a for loop, and provides some support for
combining queues.
__len__(self)
Returns the number of objects waiting on the queue.
close(self)
An event queue can be closed.  This will cause waiting to raise
the StopIteration exception (once existing entries have been read),
and will prevent any further signals to the queue.
next(self)

Data and other attributes inherited from EventBase:
__dict__ = <dictproxy object>
dictionary for instance variables (if defined)
__weakref__ = <attribute '__weakref__' of 'EventBase' objects>
list of weak references to the object (if defined)

 
class Spawn(EventBase)
    This class is used to wrap cooperative threads: every task (except
for main) managed by the scheduler should be an instance of this class.
 
 
Method resolution order:
Spawn
EventBase
__builtin__.object

Methods defined here:
AbortWait(self)
Called instead of performing a proper wait to release any resources
that might be consumed until the wait occurs.
Wait(self, timeout=None)
Waits until the task has completed.  May raise an exception if the
task terminated with an exception and raise_on_wait was selected.
Can only be called once, as the result is deleted after call.
__init__(self, function, *args, **kargs)
The given function and arguments will be called as a new task.
All of the arguments will be be passed through to function, except for
the special keyword raise_on_wait which defaults to False.
    If raise_on_wait is set then any exception raised during the
execution of this task will be postponed until Wait() is called.  This
allows such exceptions to be caught without disturbing the normal
operation of the system.  Otherwise any exception is reported.

Properties defined here:
finished
get lambda self

Data and other attributes inherited from EventBase:
__dict__ = <dictproxy object>
dictionary for instance variables (if defined)
__weakref__ = <attribute '__weakref__' of 'EventBase' objects>
list of weak references to the object (if defined)

 
class ThreadedEventQueue(__builtin__.object)
    An event queue designed to work with threads.
 
  Methods defined here:
Signal(self, value)
Posts a value to the event queue.  This can safely be called from
a thread or a cothread.
Wait(self, timeout=None)
Waits for a value to be written to the queue.  This can safely be
called from either a cothread or another thread: the appropriate form
of cooperative or normal blocking will be selected automatically.
__init__(self)

Data and other attributes defined here:
__dict__ = <dictproxy object>
dictionary for instance variables (if defined)
__weakref__ = <attribute '__weakref__' of 'ThreadedEventQueue' objects>
list of weak references to the object (if defined)

 
class Timedout(exceptions.Exception)
    Waiting for event timed out.
 
  Methods inherited from exceptions.Exception:
__getitem__(...)
__init__(...)
__str__(...)

 
class Timer(__builtin__.object)
    A cancellable one-shot or auto-retriggering timer.
 
  Methods defined here:
__init__(self, timeout, callback, retrigger=False)
The callback will be called after the specified timeout.  If
retrigger is set then the timer will automatically retrigger until
it is cancelled.
cancel(self)
Cancels the timer: the timer is guaranteed not to fire once this
call has been made.
reset(self, timeout=None, retrigger=False)
Resets the timer.  If it hasn't fired yet then the timeout is reset
to the given timeout (or its original timeout by default).  ???

Data and other attributes defined here:
__dict__ = <dictproxy object>
dictionary for instance variables (if defined)
__weakref__ = <attribute '__weakref__' of 'Timer' objects>
list of weak references to the object (if defined)

 
Functions
       
AbsTimeout(timeout)
A timeout is represented in one of three forms:
 
None            A timeout that never expires
interval        A relative timeout interval
(deadline,)     An absolute deadline
 
This routine checks that the given input is in one of these three forms
and returns a timeout in absolute deadline format.
Quit()
Signals the quit event.  Once signalled it stays signalled.
Sleep(timeout)
Sleep until the specified timeout has expired.
SleepUntil(deadline)
Sleep until the specified deadline.  Note that if the deadline has
already passed then no yield of control will occur.
WaitForAll(event_list, timeout=None)
Waits for all events in the event list to become ready or for the
timeout to expire.
WaitForQuit(catch_interrupt=True)
Waits for the quit event to be signalled.
Yield(timeout=0)
Hands control back to the scheduler.  Control is returned either after
the specified timeout has passed, or as soon as there are no active jobs
waiting to be run.

 
Data
        __all__ = ['Spawn', 'Sleep', 'SleepUntil', 'Yield', 'Event', 'EventQueue', 'ThreadedEventQueue', 'WaitForAll', 'AbsTimeout', 'Timedout', 'Quit', 'WaitForQuit', 'Timer']