scheduler.py
author Fabien Ninoles <fabien@tzone.org>
Mon, 07 Jun 2010 23:56:53 -0400
changeset 6 6657247ddbbf
parent 5 eb1133af54ed
permissions -rwxr-xr-x
Add slightly better command line handling.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     1
#!/usr/bin/env python2.6
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     2
import sys
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     3
import logging
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     4
import threading
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     5
import Queue
2
a00ae018daf8 Separate thread pool in another file and fix some issues.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
     6
from threadpool import ThreadPool
5
eb1133af54ed Seperate task and scheduler.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
     7
from task import Task, ThreadedTask
4
76ba9b3a9e1c Add GrabResult and SetupEvent, for synchronicity.
Fabien Ninoles <fabien@tzone.org>
parents: 3
diff changeset
     8
0
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     9
class Scheduler(threading.Thread):
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    10
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    11
    class _SchedulerStop(Exception):
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    12
        pass
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    13
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    14
    def __init__(self, poolSize):
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    15
        threading.Thread.__init__(self, name = "Scheduler", target = self.Run)
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    16
        self.pool = ThreadPool(poolSize)
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    17
        self.tasks = Queue.Queue()
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    18
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    19
    def ExecuteOne(self, blocking = True):
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    20
        logging.debug("Looking for next task...")
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    21
        try:
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    22
            task = self.tasks.get(blocking)
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    23
        except Queue.Empty:
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    24
            logging.debug("No task to run")
3
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    25
            return None
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    26
        result = None
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    27
        error = None
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    28
        traceback = None
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    29
        while True:
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    30
            cb = task._GetNext()
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    31
            if not cb:
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    32
                # no more callback
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    33
                break
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    34
            if cb.threaded:
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    35
                # a threaded callback
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    36
                self._AddJob(task, cb, result, error, traceback)
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    37
                # don't pass Go, don't reclaim $200
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    38
                return None
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    39
            # Run the callback according to the current state
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    40
            try:
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    41
                if error:
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    42
                    error = cb.errorback(error)
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    43
                else:
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    44
                    result = cb.callback(result)
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    45
            except:
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    46
                errtype, error, traceback = sys.exc_info()
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    47
        if error:
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    48
            raise error, None, traceback
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    49
        else:
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    50
            return result
0
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    51
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    52
    def Run(self):
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    53
        logging.info("Scheduler start")
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    54
        while True:
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    55
            try:
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    56
                self.ExecuteOne()
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    57
            except self._SchedulerStop:
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    58
                break
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    59
            except:
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    60
                logging.exception("Unhandled task exception")
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    61
        logging.info("Scheduler stop")
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    62
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    63
    def Start(self):
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    64
        self.pool.Start()
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    65
        return self.start()
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    66
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    67
    def Stop(self, now = False):
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    68
        self.pool.Stop(now)
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    69
        if now:
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    70
            self.tasks = Queue.Queue()
3
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    71
        # We raise an exception to find if we stop stop the scheduler.
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    72
        # We could have use a None task, but this make it easier if we
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    73
        # want to add such mechanism public or we want to stop on
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    74
        # other exception
0
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    75
        def RaiseSchedulerStop():
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    76
            raise self._SchedulerStop
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    77
        self.AddTask(Task(RaiseSchedulerStop))
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    78
        self.join()
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    79
4
76ba9b3a9e1c Add GrabResult and SetupEvent, for synchronicity.
Fabien Ninoles <fabien@tzone.org>
parents: 3
diff changeset
    80
    def AddTask(self, task):
76ba9b3a9e1c Add GrabResult and SetupEvent, for synchronicity.
Fabien Ninoles <fabien@tzone.org>
parents: 3
diff changeset
    81
        self.tasks.put(task)
0
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    82
3
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    83
    def _AddJob(self, task, cb, result, error, traceback):
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    84
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    85
        def DoIt(task, cb, result, error, traceback):
0
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    86
            try:
3
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    87
                if error:
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    88
                    error = cb.errorback(error)
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    89
                else:
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    90
                    result = cb.callback(result)
0
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    91
            except:
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    92
                errtype, error, traceback = sys.exc_info()
3
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    93
            if error:
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    94
                def RaiseError():
0
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    95
                    raise error, None, traceback
3
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    96
                jobTask = Task(RaiseError)
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    97
            else:
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    98
                def ReturnResult():
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    99
                    return result
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   100
                jobTask = Task(ReturnResult)
0
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   101
            jobTask.ChainTask(task)
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   102
            self.AddTask(jobTask)
3
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   103
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   104
        # This double wrap (Job over DoIt) seems necessary to make
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   105
        # error not look like a local of Job...
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   106
        def Job():
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   107
            return DoIt(task, cb, result, error, traceback)
0
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   108
        self.pool.AddJob(Job)
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   109
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   110
# The global scheduler
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   111
scheduler = None
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   112
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   113
def StartScheduler(size):
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   114
    global scheduler
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   115
    if scheduler:
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   116
        StopScheduler()
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   117
    scheduler = Scheduler(size)
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   118
    scheduler.Start()
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   119
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   120
def StopScheduler(now = False):
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   121
    global scheduler
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   122
    if scheduler:
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   123
        scheduler.Stop(now)
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   124
    scheduler = None
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   125
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   126
if __name__ == '__main__':
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   127
    from time import sleep
2
a00ae018daf8 Separate thread pool in another file and fix some issues.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   128
    logging.getLogger().setLevel(logging.INFO)
6
6657247ddbbf Add slightly better command line handling.
Fabien Ninoles <fabien@tzone.org>
parents: 5
diff changeset
   129
    if len(sys.argv) == 1:
6657247ddbbf Add slightly better command line handling.
Fabien Ninoles <fabien@tzone.org>
parents: 5
diff changeset
   130
    	numberOfTasks = 100
6657247ddbbf Add slightly better command line handling.
Fabien Ninoles <fabien@tzone.org>
parents: 5
diff changeset
   131
    else:
6657247ddbbf Add slightly better command line handling.
Fabien Ninoles <fabien@tzone.org>
parents: 5
diff changeset
   132
        numberOfTasks = int(sys.argv[1])
0
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   133
    # This function is a sample and shouldn't know about the scheduler
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   134
    count = 0
2
a00ae018daf8 Separate thread pool in another file and fix some issues.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   135
    def AsyncCall(name, seconds):
0
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   136
        global count
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   137
        count += 1
3
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   138
        
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   139
        # Probably a bad example, since the callback
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   140
        # doesn't return the exact same type...
2
a00ae018daf8 Separate thread pool in another file and fix some issues.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   141
        def Initialize(name, seconds):
0
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   142
            print "Here", name
2
a00ae018daf8 Separate thread pool in another file and fix some issues.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   143
            return name, seconds
0
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   144
        def Blocking(args):
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   145
            name, time = args
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   146
            print name, "goes to bed"
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   147
            sleep(time)
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   148
            print name, ": ZZZ..."
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   149
            return name
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   150
        def Finalize(name):
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   151
            global count
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   152
            print name, "wakes up!"
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   153
            count -= 1
4
76ba9b3a9e1c Add GrabResult and SetupEvent, for synchronicity.
Fabien Ninoles <fabien@tzone.org>
parents: 3
diff changeset
   154
            return name
0
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   155
3
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   156
        task = Task(Initialize, name, seconds)
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   157
        task.AddThreadedCallback(Blocking)
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   158
        task.AddCallback(Finalize)
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   159
        return task
0
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   160
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   161
    logging.info("Starting scheduler with 10 workers")
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   162
    StartScheduler(10)
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   163
    logging.info("Adding asynccall task")
6
6657247ddbbf Add slightly better command line handling.
Fabien Ninoles <fabien@tzone.org>
parents: 5
diff changeset
   164
    for x in xrange(numberOfTasks):
3
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   165
        task = AsyncCall("Toto%d" % (x+1), (x % 10)/10.0)
00b6708d1852 Rewrite the whole to use linked chain of callback, allowing the
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   166
        scheduler.AddTask(task)
0
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   167
    while count > 0:
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   168
        logging.debug("Count = %d", count)
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   169
        sleep(1)
4
76ba9b3a9e1c Add GrabResult and SetupEvent, for synchronicity.
Fabien Ninoles <fabien@tzone.org>
parents: 3
diff changeset
   170
76ba9b3a9e1c Add GrabResult and SetupEvent, for synchronicity.
Fabien Ninoles <fabien@tzone.org>
parents: 3
diff changeset
   171
    # Check for King Toto sleep
76ba9b3a9e1c Add GrabResult and SetupEvent, for synchronicity.
Fabien Ninoles <fabien@tzone.org>
parents: 3
diff changeset
   172
    task = AsyncCall("King Toto", 5)
76ba9b3a9e1c Add GrabResult and SetupEvent, for synchronicity.
Fabien Ninoles <fabien@tzone.org>
parents: 3
diff changeset
   173
    data = task.GrabResult()
76ba9b3a9e1c Add GrabResult and SetupEvent, for synchronicity.
Fabien Ninoles <fabien@tzone.org>
parents: 3
diff changeset
   174
    event = task.SetupEvent()
76ba9b3a9e1c Add GrabResult and SetupEvent, for synchronicity.
Fabien Ninoles <fabien@tzone.org>
parents: 3
diff changeset
   175
    scheduler.AddTask(task)
76ba9b3a9e1c Add GrabResult and SetupEvent, for synchronicity.
Fabien Ninoles <fabien@tzone.org>
parents: 3
diff changeset
   176
    try:
76ba9b3a9e1c Add GrabResult and SetupEvent, for synchronicity.
Fabien Ninoles <fabien@tzone.org>
parents: 3
diff changeset
   177
        event.wait(10)
76ba9b3a9e1c Add GrabResult and SetupEvent, for synchronicity.
Fabien Ninoles <fabien@tzone.org>
parents: 3
diff changeset
   178
        print "data = %r" % (data,)
76ba9b3a9e1c Add GrabResult and SetupEvent, for synchronicity.
Fabien Ninoles <fabien@tzone.org>
parents: 3
diff changeset
   179
    except:
76ba9b3a9e1c Add GrabResult and SetupEvent, for synchronicity.
Fabien Ninoles <fabien@tzone.org>
parents: 3
diff changeset
   180
        logging.exception("Error occured on wait")
0
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   181
    logging.info("Stopping scheduler")
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   182
    StopScheduler()
8ae7370093db First version.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   183
    logging.info("The End.")