/usr/lib/python2.7/dist-packages/meld/task.py is in meld 3.14.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | # Copyright (C) 2002-2006 Stephen Kennedy <stevek@gnome.org>
# Copyright (C) 2012-2013 Kai Willadsen <kai.willadsen@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Classes to implement scheduling for cooperative threads."""
from __future__ import print_function
import traceback
class SchedulerBase(object):
"""Base class with common functionality for schedulers
Derived classes must implement get_current_task.
"""
def __init__(self):
self.tasks = []
self.callbacks = []
def __repr__(self):
return "%s" % self.tasks
def connect(self, signal, action):
assert signal == "runnable"
if action not in self.callbacks:
self.callbacks.append(action)
def add_task(self, task, atfront=False):
"""Add a task to the scheduler's task list
The task may be a function, generator or scheduler, and is
deemed to have finished when it returns a false value or raises
StopIteration.
"""
self.remove_task(task)
if atfront:
self.tasks.insert(0, task)
else:
self.tasks.append(task)
for callback in self.callbacks:
callback(self)
def remove_task(self, task):
"""Remove a single task from the scheduler"""
try:
self.tasks.remove(task)
except ValueError:
pass
def remove_all_tasks(self):
"""Remove all tasks from the scheduler"""
self.tasks = []
def add_scheduler(self, sched):
"""Adds a subscheduler as a child task of this scheduler"""
sched.connect("runnable", lambda t: self.add_task(t))
def remove_scheduler(self, sched):
"""Remove a sub-scheduler from this scheduler"""
self.remove_task(sched)
try:
self.callbacks.remove(sched)
except ValueError:
pass
def get_current_task(self):
"""Overridden function returning the next task to run"""
raise NotImplementedError
def __call__(self):
"""Run an iteration of the current task"""
if len(self.tasks):
r = self.iteration()
if r:
return r
return self.tasks_pending()
def complete_tasks(self):
"""Run all of the scheduler's current tasks to completion"""
while self.tasks_pending():
self.iteration()
def tasks_pending(self):
return len(self.tasks) != 0
def iteration(self):
"""Perform one iteration of the current task"""
try:
task = self.get_current_task()
except StopIteration:
return 0
try:
if hasattr(task, "__iter__"):
ret = next(task)
else:
ret = task()
except StopIteration:
pass
except Exception:
traceback.print_exc()
else:
if ret:
return ret
self.tasks.remove(task)
return 0
class LifoScheduler(SchedulerBase):
"""Scheduler calling most recently added tasks first"""
def get_current_task(self):
try:
return self.tasks[-1]
except IndexError:
raise StopIteration
class FifoScheduler(SchedulerBase):
"""Scheduler calling tasks in the order they were added"""
def get_current_task(self):
try:
return self.tasks[0]
except IndexError:
raise StopIteration
if __name__ == "__main__":
import time
import random
m = LifoScheduler()
def timetask(t):
while time.time() - t < 1:
print("***")
time.sleep(0.1)
print("!!!")
def sayhello(x):
for i in range(random.randint(2, 8)):
print("hello", x)
time.sleep(0.1)
yield 1
print("end", x)
s = FifoScheduler()
m.add_task(s)
s.add_task(sayhello(10))
s.add_task(sayhello(20))
s.add_task(sayhello(30))
while s.tasks_pending():
s.iteration()
time.sleep(2)
print("***")
|