Support ignore_class and queue to noparallel decorator

This commit is contained in:
shortcutme 2017-04-01 22:31:15 +02:00
parent cfb2276ecd
commit 233d5f31f2
No known key found for this signature in database
GPG key ID: 5B63BAE6CB9613AE
2 changed files with 49 additions and 3 deletions

View file

@ -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