Last active
August 29, 2015 14:14
-
-
Save MadFaill/e112435f87efb4bf17d9 to your computer and use it in GitHub Desktop.
greenclock add stop task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import greenclock | |
class Scheduler(greenclock.Scheduler): | |
pool = None | |
greenlets = {} | |
def run_tasks(self): | |
''' | |
Runs all assigned task in separate green threads. If the task should not be run, schedule it | |
''' | |
self.pool = Pool(len(self.tasks)) | |
for task in self.tasks: | |
# Launch a green thread to schedule the task | |
# A task will be managed by 2 green thread: execution thread and scheduling thread | |
self.pool.spawn(self.do_run, task) | |
return self.pool | |
def do_run(self, task): | |
greenlet, next_greenlet = self.run(task) | |
self.greenlets.update({task.name: [greenlet, next_greenlet]}) | |
return greenlet, next_greenlet | |
def stop_task(self, name): | |
for key, task in enumerate(self.tasks): | |
print key, task.name | |
if task.name == name: | |
greens = self.greenlets.pop(task.name, []) | |
for g in greens: | |
if g is not None: | |
self.tasks.pop(key) | |
self.pool.killone(g) | |
self.pool.discard(g) | |
g.unlink(task.action) | |
g.kill() | |
print "Stopped task: %s" % task.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment