Exemplo n.º 1
0
class Worker(Thread):
    """request new task to execute from ThreadPool"""
    def __init__(self, thread_pool, worker_id):
        """initial data and start myself as a new thread"""
        Thread.__init__(self)
        self._pool = thread_pool
        self._woker_id = worker_id
        self._is_dying = False
        self._work_times = 0
        self._rest_time = 0.01
        self._log = Log()

    def run(self):
        """ask and do works from task pool"""
        while (self._is_dying == False):
            """get the function name, parameter and the callback function"""
            func, args, callback = self._pool.get_new_task()
            if func == None:  #no task, have a rest
                sleep(self._rest_time)
            else:
                try:
                    """run the function with its parameter and the callback function"""
                    func(args, callback)
                except (Exception) as e:
                    self._log.debug(e)
                    #sys.exit(1)
                ++self._work_times

    def goaway(self):
        """ stop myself, this lead to the end of the run function, which will kill the thread"""
        self._is_dying = True
Exemplo n.º 2
0
class Worker(Thread):
    """request new task to execute from ThreadPool"""

    def __init__(self, thread_pool, worker_id):
        """initial data and start myself as a new thread""" 
        Thread.__init__(self)
        self._pool          = thread_pool
        self._woker_id      = worker_id;
        self._is_dying      = False
        self._work_times    = 0
        self._rest_time     = 0.01
        self._log           = Log()
        

    def run(self):
        """ask and do works from task pool"""
        while (self._is_dying == False):
            """get the function name, parameter and the callback function"""
            func, args, callback = self._pool.get_new_task()      
            if func == None: #no task, have a rest
                sleep(self._rest_time)
            else:
                try:
                    """run the function with its parameter and the callback function"""
                    func(args, callback)
                except (Exception) as e:  
                    self._log.debug(e)
                    #sys.exit(1)
                ++self._work_times

    def goaway(self):
        """ stop myself, this lead to the end of the run function, which will kill the thread"""
        self._is_dying = True
Exemplo n.º 3
0
 def __init__(self, thread_pool, worker_id):
     """initial data and start myself as a new thread"""
     Thread.__init__(self)
     self._pool = thread_pool
     self._woker_id = worker_id
     self._is_dying = False
     self._work_times = 0
     self._rest_time = 0.01
     self._log = Log()
Exemplo n.º 4
0
 def __init__(self, thread_pool, worker_id):
     """initial data and start myself as a new thread""" 
     Thread.__init__(self)
     self._pool          = thread_pool
     self._woker_id      = worker_id;
     self._is_dying      = False
     self._work_times    = 0
     self._rest_time     = 0.01
     self._log           = Log()