Skip to content

Commit

Permalink
Some perf related changes when profiling the MI Task example within s…
Browse files Browse the repository at this point in the history
…piff-arena (#423)

* WIP

* WIP

* WIP
  • Loading branch information
jbirddog authored Jul 1, 2024
1 parent ed6c428 commit 7392ec6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
30 changes: 17 additions & 13 deletions SpiffWorkflow/bpmn/util/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@ def matches(self, task):
def _catches_event(task):
return isinstance(task.task_spec, CatchingEvent) and task.task_spec.catches(task, self.catches_event)

return all([
super().matches(task),
self.catches_event is None or _catches_event(task),
self.lane is None or task.task_spec.lane == self.lane,
])
if not super().matches(task):
return False

if not (self.catches_event is None or _catches_event(task)):
return False

if not (self.lane is None or task.task_spec.lane == self.lane):
return False

return True


class BpmnTaskIterator(TaskIterator):
Expand All @@ -49,18 +54,17 @@ def __init__(self, task, end_at_spec=None, max_depth=1000, depth_first=True, ski

def _next(self):

if len(self.task_list) == 0:
if not self.task_list:
raise StopIteration()

task = self.task_list.pop(0)
subprocess = task.workflow.top_workflow.subprocesses.get(task.id)

if all([
len(task._children) > 0 or subprocess is not None,
task.state >= self.min_state or subprocess is not None,
self.depth < self.max_depth,
task.task_spec.name != self.end_at_spec,
]):
if (task._children or subprocess is not None) and \
(task.state >= self.min_state or subprocess is not None) and \
self.depth < self.max_depth and \
task.task_spec.name != self.end_at_spec:

# Do not descend into a completed subprocess to look for unfinished tasks.
if (
subprocess is None
Expand All @@ -80,7 +84,7 @@ def _next(self):

self._update_depth(task)

elif self.depth_first and len(self.task_list) > 0:
elif self.depth_first and self.task_list:
self._handle_leaf_depth(task)

return task
2 changes: 1 addition & 1 deletion SpiffWorkflow/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def has_state(self, state):
Returns:
bool: `True` is the task has the state or mask
"""
return (self.state & state) != 0
return (self._state & state) != 0

def set_data(self, **kwargs):
"""Defines the given attribute/value pairs in this task's data."""
Expand Down
40 changes: 25 additions & 15 deletions SpiffWorkflow/util/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,25 @@ def matches(self, task):
Returns:
bool: indicates whether the task matches
"""
return all([
task.has_state(self.state),
task.last_state_change >= self.updated_ts,
self.manual is None or task.task_spec.manual == self.manual,
self.spec_name is None or task.task_spec.name == self.spec_name,
self.spec_class is None or isinstance(task.task_spec, self.spec_class),
])

if not task.has_state(self.state):
return False

if not task.last_state_change >= self.updated_ts:
return False

if not (self.manual is None or task.task_spec.manual == self.manual):
return False

if not (self.spec_name is None or task.task_spec.name == self.spec_name):
return False

if not (self.spec_class is None or isinstance(task.task_spec, self.spec_class)):
return False


return True


class TaskIterator:
"""Default task iteration class."""
Expand Down Expand Up @@ -214,22 +225,21 @@ def __next__(self):

def _next(self):

if len(self.task_list) == 0:
if not self.task_list:
raise StopIteration()

task = self.task_list.pop(0)
if all([
len(task._children) > 0,
task.state >= self.min_state,
self.depth < self.max_depth,
task.task_spec.name != self.end_at_spec,
]):
if task._children and \
task.state >= self.min_state and \
self.depth < self.max_depth and \
task.task_spec.name != self.end_at_spec:

if self.depth_first:
self.task_list = task.children + self.task_list
else:
self.task_list.extend(task.children)
self._update_depth(task)
elif self.depth_first and len(self.task_list) > 0:
elif self.depth_first and self.task_list:
self._handle_leaf_depth(task)

return task
Expand Down
4 changes: 4 additions & 0 deletions SpiffWorkflow/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def __init__(self, workflow_spec, deserializing=False):
self.last_task = None
self.success = True
self.tasks = {}
self.completed = False

# Events.
self.completed_event = Event()
Expand All @@ -72,10 +73,13 @@ def is_completed(self):
Returns:
bool: True if the workflow has no unfinished tasks
"""
if self.completed:
return True
iter = TaskIterator(self.task_tree, state=TaskState.NOT_FINISHED_MASK)
try:
next(iter)
except StopIteration:
self.completed = True
return True
return False

Expand Down

0 comments on commit 7392ec6

Please sign in to comment.