ME 405 Romi
Loading...
Searching...
No Matches
cotask.Task Class Reference

Implements multitasking with scheduling and some performance logging. More...

Public Member Functions

 __init__ (self, run_fun, name="NoName", priority=0, period=None, profile=False, trace=False, shares=())
 Initialize a task object so it may be run by the scheduler.
bool schedule (self)
 This method is called by the scheduler; it attempts to run this task.
bool ready (self)
 This method checks if the task is ready to run.
 set_period (self, new_period)
 This method sets the period between runs of the task to the given number of milliseconds, or None if the task is triggered by calls to go() rather than time.
 reset_profile (self)
 This method resets the variables used for execution time profiling.
 get_trace (self)
 This method returns a string containing the task's transition trace.
 go (self)
 Method to set a flag so that this task indicates that it's ready to run.
 __repr__ (self)
 This method converts the task to a string for diagnostic use.

Public Attributes

 name = name
 The name of the task, hopefully a short and descriptive string.
 priority = int(priority)
 The task's priority, an integer with higher numbers meaning higher priority.
int period = int(period * 1000)
bool go_flag = False
 Flag which is set true when the task is ready to be run by the scheduler.

Protected Attributes

 _run_gen = run_fun(shares)
 _next_run = utime.ticks_us() + self.period
 _prof = profile
int _prev_state = 0
bool _trace = trace
list _tr_data = []
 _prev_time = utime.ticks_us()
int _slowest
int _latest
int _runs = 0
int _run_sum = 0
int _late_sum = 0

Detailed Description

Implements multitasking with scheduling and some performance logging.

This class implements behavior common to tasks in a cooperative multitasking system which runs in MicroPython. The ability to be scheduled on the basis of time or an external software trigger or interrupt is implemented, state transitions can be recorded, and run times can be profiled. The user's task code must be implemented in a generator which yields the state (and the CPU) after it has run for a short and bounded period of time.

Example:

def task1_fun ():
'''! This function switches states repeatedly for no reason '''
state = 0
while True:
if state == 0:
state = 1
elif state == 1:
state = 0
yield (state)
# In main routine, create this task and set it to run twice per second
task1 = cotask.Task (task1_fun, name = 'Task 1', priority = 1,
period = 500, profile = True, trace = True)
# Add the task to the list (so it will be run) and run scheduler
cotask.task_list.append (task1)
while True:
cotask.task_list.pri_sched ()
Implements multitasking with scheduling and some performance logging.
Definition cotask.py:66

Definition at line 66 of file cotask.py.

Constructor & Destructor Documentation

◆ __init__()

cotask.Task.__init__ ( self,
run_fun,
name = "NoName",
priority = 0,
period = None,
profile = False,
trace = False,
shares = () )

Initialize a task object so it may be run by the scheduler.

This method initializes a task object, saving copies of constructor parameters and preparing an empty dictionary for states.

Parameters
run_funThe function which implements the task's code. It must be a generator which yields the current state.
nameThe name of the task, by default NoName. This should be overridden with a more descriptive name by the programmer.
priorityThe priority of the task, a positive integer with higher numbers meaning higher priority (default 0)
periodThe time in milliseconds between runs of the task if it's run by a timer or None if the task is not run by a timer. The time can be given in a float or int; it will be converted to microseconds for internal use by the scheduler.
profileSet to True to enable run-time profiling
traceSet to True to generate a list of transitions between states. Note: This slows things down and allocates memory.
sharesA list or tuple of shares and queues used by this task. If no list is given, no shares are passed to the task

Definition at line 88 of file cotask.py.

Member Function Documentation

◆ __repr__()

cotask.Task.__repr__ ( self)

This method converts the task to a string for diagnostic use.

It shows information about the task, including execution time profiling results if profiling has been done.

Returns
The string which represents the task

Definition at line 269 of file cotask.py.

◆ get_trace()

cotask.Task.get_trace ( self)

This method returns a string containing the task's transition trace.

The trace is a set of tuples, each of which contains a time and the states from and to which the system transitioned.

Returns
A possibly quite large string showing state transitions

Definition at line 242 of file cotask.py.

◆ go()

cotask.Task.go ( self)

Method to set a flag so that this task indicates that it's ready to run.

This method may be called from an interrupt service routine or from another task which has data that this task needs to process soon.

Definition at line 261 of file cotask.py.

◆ ready()

bool cotask.Task.ready ( self)

This method checks if the task is ready to run.

If the task runs on a timer, this method checks what time it is; if not, this method checks the flag which indicates that the task is ready to go. This method may be overridden in descendent classes to implement some other behavior.

Definition at line 197 of file cotask.py.

Here is the caller graph for this function:

◆ reset_profile()

cotask.Task.reset_profile ( self)

This method resets the variables used for execution time profiling.

This method is also used by __init__() to create the variables.

Definition at line 230 of file cotask.py.

◆ schedule()

bool cotask.Task.schedule ( self)

This method is called by the scheduler; it attempts to run this task.

If the task is not yet ready to run, this method returns False immediately; if this task is ready to run, it runs the task's generator up to the next yield() and then returns True.

Returns
True if the task ran or False if it did not

Definition at line 143 of file cotask.py.

Here is the call graph for this function:

◆ set_period()

cotask.Task.set_period ( self,
new_period )

This method sets the period between runs of the task to the given number of milliseconds, or None if the task is triggered by calls to go() rather than time.

Parameters
new_periodThe new period in milliseconds between task runs

Definition at line 221 of file cotask.py.

Member Data Documentation

◆ _late_sum

int cotask.Task._late_sum = 0
protected

Definition at line 234 of file cotask.py.

◆ _latest

int cotask.Task._latest
protected

Definition at line 210 of file cotask.py.

◆ _next_run

cotask.Task._next_run = utime.ticks_us() + self.period
protected

Definition at line 112 of file cotask.py.

◆ _prev_state

int cotask.Task._prev_state = 0
protected

Definition at line 124 of file cotask.py.

◆ _prev_time

cotask.Task._prev_time = utime.ticks_us()
protected

Definition at line 130 of file cotask.py.

◆ _prof

cotask.Task._prof = profile
protected

Definition at line 119 of file cotask.py.

◆ _run_gen

cotask.Task._run_gen = run_fun(shares)
protected

Definition at line 94 of file cotask.py.

◆ _run_sum

int cotask.Task._run_sum = 0
protected

Definition at line 232 of file cotask.py.

◆ _runs

int cotask.Task._runs = 0
protected

Definition at line 231 of file cotask.py.

◆ _slowest

int cotask.Task._slowest
protected

Definition at line 166 of file cotask.py.

◆ _tr_data

list cotask.Task._tr_data = []
protected

Definition at line 129 of file cotask.py.

◆ _trace

bool cotask.Task._trace = trace
protected

Definition at line 128 of file cotask.py.

◆ go_flag

bool cotask.Task.go_flag = False

Flag which is set true when the task is ready to be run by the scheduler.

Definition at line 134 of file cotask.py.

◆ name

cotask.Task.name = name

The name of the task, hopefully a short and descriptive string.

Definition at line 99 of file cotask.py.

◆ period

int cotask.Task.period = int(period * 1000)

Definition at line 111 of file cotask.py.

◆ priority

cotask.Task.priority = int(priority)

The task's priority, an integer with higher numbers meaning higher priority.

Definition at line 103 of file cotask.py.


The documentation for this class was generated from the following file: