root/trunk/lib/threads.py

Revision 15, 2.4 kB (checked in by mjoc, 1 year ago)

Atnaujinti vertimai. Pridėti nauji copyright'ai. Pakeista konfigūracija.

Line 
1 #
2 # OpenDict
3 # Copyright (c) 2003-2006 Martynas Jocius <martynas.jocius@idiles.com>
4 # Copyright (c) 2007 IDILES SYSTEMS, UAB <support@idiles.com>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your opinion) any later version.
10 #
11 # This program is distributed in the hope that will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MECHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more detals.
15 #
16 # You shoud have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # 02111-1307 USA
20 #
21 # Module: threads
22
23 from threading import *
24 import sys
25 import os
26 import copy
27 import traceback
28 import string
29
30 class KThread(Thread):
31     """Thread that can be killed during its run()"""
32
33     def join(self, timeout=None):
34         """Kill it"""
35
36         Thread.join(self, timeout)
37         print "Thread killing himself"
38         #os._exit(0)
39
40
41 class Process:
42
43     def __init__(self, func, *param):
44         self.__done = 0
45         self.__result = None
46         self.__status = "working"
47
48         self.__C = Condition()
49
50         # Seperate thread
51         self.__T = Thread(target=self.Wrapper, args=(func, param))
52         self.__T.setName("ProcessThread")
53         self.__T.start()
54
55     def __repr__(self):
56         return "<Process at "+hex(id(self))+":"+self.__status+">"
57
58     def __call__(self):
59         self.__C.acquire()
60         while self.__done == 0:
61             self.__C.wait()
62         self.__C.release()
63
64         result = copy.copy(self.__result)
65         return result
66
67     def isDone(self):
68         return self.__done
69
70     def stop(self):
71         # FIXME: this actually doesn't kill the running process
72         # Needs to be done. Help?
73         #Thread.join(self.__T, None)
74         self.__T.join(0)
75
76     def Wrapper(self, func, param):
77         self.__C.acquire()
78         try:
79            self.__result = func(*param)
80         except:
81            self.__result = None
82            print string.join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1],
83            sys.exc_info()[2]), "")
84         self.__done = 1
85         self.__status = "self.__result"
86         self.__C.notify()
87         self.__C.release()
88
Note: See TracBrowser for help on using the browser.