Support ignore_class and queue to noparallel decorator
This commit is contained in:
parent
cfb2276ecd
commit
233d5f31f2
2 changed files with 49 additions and 3 deletions
|
@ -14,6 +14,13 @@ class ExampleClass(object):
|
|||
self.counted += 1
|
||||
return "counted:%s" % i
|
||||
|
||||
@util.Noparallel(queue=True, ignore_class=True)
|
||||
def countQueue(self, num=5):
|
||||
for i in range(1, num+1):
|
||||
time.sleep(0.01)
|
||||
self.counted += 1
|
||||
return "counted:%s" % i
|
||||
|
||||
@util.Noparallel(blocking=False)
|
||||
def countNoblocking(self, num=5):
|
||||
for i in range(1, num+1):
|
||||
|
@ -57,3 +64,34 @@ class TestNoparallel:
|
|||
|
||||
obj1.countNoblocking().join() # Allow again and wait until finishes
|
||||
assert obj1.counted == 10
|
||||
|
||||
def testQueue(self):
|
||||
obj1 = ExampleClass()
|
||||
|
||||
threads = [
|
||||
gevent.spawn(obj1.countQueue),
|
||||
gevent.spawn(obj1.countQueue),
|
||||
gevent.spawn(obj1.countQueue)
|
||||
]
|
||||
gevent.joinall(threads)
|
||||
|
||||
assert obj1.counted == 15 # Calls should be executed sequentially
|
||||
|
||||
def testIngoreClass(self):
|
||||
obj1 = ExampleClass()
|
||||
obj2 = ExampleClass()
|
||||
|
||||
threads = [
|
||||
gevent.spawn(obj1.countQueue),
|
||||
gevent.spawn(obj1.countQueue),
|
||||
gevent.spawn(obj1.countQueue),
|
||||
gevent.spawn(obj2.countQueue),
|
||||
gevent.spawn(obj2.countQueue)
|
||||
]
|
||||
s = time.time()
|
||||
gevent.joinall(threads)
|
||||
assert obj1.counted == 15
|
||||
assert obj2.counted == 10
|
||||
|
||||
taken = time.time() - s
|
||||
assert taken >= 0.25 # Every count takes 0.05sec
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue