File: inheritable.py

package info (click to toggle)
python-extended-threading 0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 124 kB
  • ctags: 139
  • sloc: python: 867; makefile: 32
file content (181 lines) | stat: -rw-r--r-- 4,675 bytes parent folder | download | duplicates (2)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#
# This module was written in 2007 by S James S Stapleton
# This module is released as PUBLIC DOMAIN
#
# Please respect the creator/author's intent and release all
# modifications as public domain. Thank you.
#



#
# This provides an inheritable lock, rlock, event and condition object set. I didn't put semaphores in here because they strike me
# dangerous, but feel free to add them in your own. Obviously it won't be that challanging. If you do make additions, please
# release them and keep them public domain in the spirit of the code here.
#



try: import threading as threading
except Exception, e:
    try: import dummy_threading as threading
    except Exception, f:
#        print "Failed to import threading: ", e
#        print "Failed to import dummy threading: ", f
#        print "exiting"
        raise Excepion(str(e) + "\n\n" + str(f))




class Ilock(object):
    """
So, I was writing a class that best inherited from a lock object. Python wouldn't let you do this. It said the thread.LockObject,
which you get from a threading.Lock() call and is actually a thread.lock is not inheritable.

Screw that.

This interfaces just as the result of a threading.Lock() would, but possibly with a bit of a performance penalty.
    """

    __lock   = None

    def __init__(self):
        self.__lock = threading.Lock()
#        print "My lock is:"
#        print type(self.__lock)
    #def __init__(self):

    def acquire(self, blocking=1):
        return self.__lock.acquire(blocking)
    #def sacquire(self, timeout=True):

    def release(self):
        return self.__lock.release()
    #def release(self):

#class Ilock(object):



class Irlock(object):
    """
So, I was writing a class that best inherited from a lock object. Python wouldn't let you do this. It said the thread.LockObject,
which you get from a threading.Lock() call and is actually a thread.lock is not inheritable.

Screw that.

This interfaces just as the result of a threading.Rlock() would, but possibly with a bit of a performance penalty.
    """

    __lock   = None

    def __init__(self):
        self.__lock = threading.RLock()
    #def __init__(self):

    def acquire(self, blocking=1):
        return self.__lock.acquire(blocking)
    #def sacquire(self, timeout=True):

    def release(self):
        return self.__lock.release()
    #def release(self):

#class Irlock(object):


class Icondition(object):
    """
So, I was writing a class that best inherited from a lock object. Python wouldn't let you do this. It said the thread.LockObject,
which you get from a threading.Lock() call and is actually a thread.lock is not inheritable.

Screw that.

This interfaces similar to the result of a threading.Condition() would, but possibly with a bit of a performance penalty. It does
have extra functionality with the notifyR and notifyAllR functions, which notify and release, instead of just notifying.

    """

    __cond   = None

    def __init__(self, lock=None):
        self.__cond = threading.Condition(lock)
    #def __init__(self, lock=None):

    def acquire(self, *args):
        return self.__cond.acquire(args)
    #def acquire(self, *args):

    def wait(self, timeout=None):
        return self.__cond.wait(timeout)
    #def wait(self, timeout=None):

    def release(self):
        self.__cond.release()
    #def release(self):

    def notify(self):
        return self.__cond.notify(self)
    #def notify(self):

    def notifyAll(self):
        return self.__cond.notifyAll(self)
    #def notifyAll(self):

    def notifyR(self):
        "Notify and release"
        self.__cond.notify(self)
        self.__cond.release()
    #def notify(self):

    def notifyAllR(self):
        "Notify all and release"
        self.__cond.notifyAll(self)
        self.__conf.release()
    #def notifyAll(self):

#class Icondition(object):



class Ievent(object):
    """
So, I was writing a class that best inherited from a lock object. Python wouldn't let you do this. It said the thread.LockObject,
which you get from a threading.Lock() call and is actually a thread.lock is not inheritable.

Screw that.

This interfaces similar to the result of a threading.Event() would, but possibly with a bit of a performance penalty.

    """

    __even   = None

    def __init__(self):
        self.__even = threading.Event()
    #def __init__(self, lock=None):

    def isSet():
        self.__even.isSet()
    #def isSet():

    def set():
        self.__even.set()
    #def set();

    def clear():
        self.__even.clear()
    #def clear():

    def wait(timeout=None):
        self.__even.wait(timeout)
    #def wait(timeout=None):

#class Ievent(Object):