File: Worker.py

package info (click to toggle)
enthought-traits-ui 2.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 15,204 kB
  • ctags: 9,623
  • sloc: python: 45,547; sh: 32; makefile: 19
file content (103 lines) | stat: -rw-r--r-- 3,439 bytes parent folder | download
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
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
# 
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license.  The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
# 
# Author: Enthought, Inc.
# Description: <Enthought util package component>
#------------------------------------------------------------------------------
import threading

class Worker(threading.Thread):
    """ Performs numerically intensive computations on a separate thread.
    
        Computations that take more than ~0.1 second should not be run in the 
        same thread as the user interface.  
        
        Typical usage:
			
        >>> worker = Worker(name = "my worker thread") 
        >>> worker.perform_work(my_function_name, args)
        >>> worker.start()                             
        >>> worker.cancel()                           
    """
    
    def __init__(self, **kwds):
        """ Passes the thread constructor a name for the thread.
		
        Naming threads makes debugging less impossible. The thread is set to be 
        a daemon thread, which means that if is the only remaining executing
        thread the program will terminate. 
        """
        self._stopevent = threading.Event()
        threading.Thread.__init__(self, **kwds)
        self.setDaemon(True)
        
    def perform_work(self, callable, *args, **kwds):
        """ Indicates to the thread the method or function and it's arguments.
        
        When the thread begins to run it will execute 'callable' and pass it 
        'args' and 'kwds'
        """
        
        self.callable = callable 
        self.args = args
        self.kwds = kwds
        
    def run(self):
        """ Private method - used only by the threading.Thread module.
        
        Users signal a thread should start to run by calling start().
        """

        self.callable(self, *self.args, **self.kwds)
        
    def cancel(self, timeout = None):
        """ Signals to the worker thread that it should stop computing.
		
            Calling this method before the thread is started generates an
            exception.
        """
        self._stopevent.set()
        threading.Thread.join(self, timeout)
        
    def abort(self):
        """ should the algorithm stop computing? 
        """
        return self._stopevent.isSet()
            
if __name__ == "__main__":
    
    import time

    def slow_eval(worker, sleep_time):   
        for i in range(10):
            if worker.abort():
                return
            else:
                # pretend to do some intensive computation 
                time.sleep(sleep_time)
                print worker.getName(),' sleeping for: ', sleep_time
        return sleep_time

    print 'the standard thread is: ', threading.currentThread 
    
    worker = Worker(name = "First EnVisage worker thread")
    worker.perform_work(slow_eval, 1)
    worker.start()   
    time.sleep(3)
    worker.cancel()
    
    worker = Worker(name = "Second EnVisage worker thread")
    worker.perform_work(slow_eval, 1)
    worker.start()
    time.sleep(2)
    worker.cancel()