File: test_ThreadLock.py

package info (click to toggle)
python-extclass 1.2-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 432 kB
  • ctags: 547
  • sloc: ansic: 4,549; python: 134; makefile: 46; sh: 18
file content (54 lines) | stat: -rw-r--r-- 973 bytes parent folder | download | duplicates (4)
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

import ThreadLock, thread
from rand import rand
from time import sleep

from ExtensionClass import Base

with_lock=1

class P(Base):

    def __oldcall_method__(self,f,a,k={}):
	if with_lock:
	    try: lock=self.lock
	    except AttributeError: return apply(f,a,k)
	else: return apply(f,a,k)
	try:
	    lock.acquire()
	    return apply(f,a,k)
	finally:
	    lock.release()

    __call_method__=apply

    def __init__(self,*args,**kw):
	self.count=0
	if with_lock:
	    self.lock=lock=ThreadLock.allocate_lock()
	    self.__call_method__=lock.guarded_apply

    def inc(self):
	    c=self.count
	    sleep(rand()/32768.0)
	    self.count=self.count+1
	    return c,self.count

    def incn(self,n):
	    c=self.count
	    for i in range(n): self.inc()
	    return c,self.count
	
p=P(1,2,spam=3)

def test():
    
    for i in range(10):
	n=3
	old,new=p.incn(n)
	print old,new
	if old+n != new: print 'oops'


for i in range(10): thread.start_new_thread(test,())
sleep(50)