Rev427, Ignore repr from coverage, Add RateLimit call penalty to, Event test, Noparallel test, RateLimit test, Remove falling Pypy test
This commit is contained in:
parent
8f7df0f7fb
commit
47dbdc0850
9 changed files with 342 additions and 133 deletions
|
@ -25,33 +25,31 @@ class Event(list):
|
|||
return self
|
||||
|
||||
|
||||
def testBenchmark():
|
||||
def say(pre, text):
|
||||
print "%s Say: %s" % (pre, text)
|
||||
|
||||
import time
|
||||
s = time.time()
|
||||
on_changed = Event()
|
||||
for i in range(1000):
|
||||
on_changed.once(lambda pre: say(pre, "once"), "once")
|
||||
print "Created 1000 once in %.3fs" % (time.time() - s)
|
||||
on_changed("#1")
|
||||
|
||||
|
||||
def testUsage():
|
||||
def say(pre, text):
|
||||
print "%s Say: %s" % (pre, text)
|
||||
|
||||
on_changed = Event()
|
||||
on_changed.once(lambda pre: say(pre, "once"))
|
||||
on_changed.once(lambda pre: say(pre, "once"))
|
||||
on_changed.once(lambda pre: say(pre, "namedonce"), "namedonce")
|
||||
on_changed.once(lambda pre: say(pre, "namedonce"), "namedonce")
|
||||
on_changed.append(lambda pre: say(pre, "always"))
|
||||
on_changed("#1")
|
||||
on_changed("#2")
|
||||
on_changed("#3")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
def testBenchmark():
|
||||
def say(pre, text):
|
||||
print "%s Say: %s" % (pre, text)
|
||||
|
||||
import time
|
||||
s = time.time()
|
||||
on_changed = Event()
|
||||
for i in range(1000):
|
||||
on_changed.once(lambda pre: say(pre, "once"), "once")
|
||||
print "Created 1000 once in %.3fs" % (time.time() - s)
|
||||
on_changed("#1")
|
||||
|
||||
def testUsage():
|
||||
def say(pre, text):
|
||||
print "%s Say: %s" % (pre, text)
|
||||
|
||||
on_changed = Event()
|
||||
on_changed.once(lambda pre: say(pre, "once"))
|
||||
on_changed.once(lambda pre: say(pre, "once"))
|
||||
on_changed.once(lambda pre: say(pre, "namedonce"), "namedonce")
|
||||
on_changed.once(lambda pre: say(pre, "namedonce"), "namedonce")
|
||||
on_changed.append(lambda pre: say(pre, "always"))
|
||||
on_changed("#1")
|
||||
on_changed("#2")
|
||||
on_changed("#3")
|
||||
|
||||
testBenchmark()
|
||||
|
|
|
@ -43,98 +43,92 @@ class Noparallel(object): # Only allow function running once in same time
|
|||
del(self.threads[key])
|
||||
|
||||
|
||||
class Test():
|
||||
|
||||
@Noparallel()
|
||||
def count(self, num=5):
|
||||
for i in range(num):
|
||||
print self, i
|
||||
time.sleep(1)
|
||||
return "%s return:%s" % (self, i)
|
||||
|
||||
|
||||
class TestNoblock():
|
||||
|
||||
@Noparallel(blocking=False)
|
||||
def count(self, num=5):
|
||||
for i in range(num):
|
||||
print self, i
|
||||
time.sleep(1)
|
||||
return "%s return:%s" % (self, i)
|
||||
|
||||
|
||||
def testBlocking():
|
||||
test = Test()
|
||||
test2 = Test()
|
||||
print "Counting..."
|
||||
print "Creating class1/thread1"
|
||||
thread1 = gevent.spawn(test.count)
|
||||
print "Creating class1/thread2 (ignored)"
|
||||
thread2 = gevent.spawn(test.count)
|
||||
print "Creating class2/thread3"
|
||||
thread3 = gevent.spawn(test2.count)
|
||||
|
||||
print "Joining class1/thread1"
|
||||
thread1.join()
|
||||
print "Joining class1/thread2"
|
||||
thread2.join()
|
||||
print "Joining class2/thread3"
|
||||
thread3.join()
|
||||
|
||||
print "Creating class1/thread4 (its finished, allowed again)"
|
||||
thread4 = gevent.spawn(test.count)
|
||||
print "Joining thread4"
|
||||
thread4.join()
|
||||
|
||||
print thread1.value, thread2.value, thread3.value, thread4.value
|
||||
print "Done."
|
||||
|
||||
|
||||
def testNoblocking():
|
||||
test = TestNoblock()
|
||||
test2 = TestNoblock()
|
||||
print "Creating class1/thread1"
|
||||
thread1 = test.count()
|
||||
print "Creating class1/thread2 (ignored)"
|
||||
thread2 = test.count()
|
||||
print "Creating class2/thread3"
|
||||
thread3 = test2.count()
|
||||
print "Joining class1/thread1"
|
||||
thread1.join()
|
||||
print "Joining class1/thread2"
|
||||
thread2.join()
|
||||
print "Joining class2/thread3"
|
||||
thread3.join()
|
||||
|
||||
print "Creating class1/thread4 (its finished, allowed again)"
|
||||
thread4 = test.count()
|
||||
print "Joining thread4"
|
||||
thread4.join()
|
||||
|
||||
print thread1.value, thread2.value, thread3.value, thread4.value
|
||||
print "Done."
|
||||
|
||||
|
||||
def testBenchmark():
|
||||
import time
|
||||
|
||||
def printThreadNum():
|
||||
import gc
|
||||
from greenlet import greenlet
|
||||
objs = [obj for obj in gc.get_objects() if isinstance(obj, greenlet)]
|
||||
print "Greenlets: %s" % len(objs)
|
||||
|
||||
printThreadNum()
|
||||
test = TestNoblock()
|
||||
s = time.time()
|
||||
for i in range(3):
|
||||
gevent.spawn(test.count, i + 1)
|
||||
print "Created in %.3fs" % (time.time() - s)
|
||||
printThreadNum()
|
||||
time.sleep(5)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
class Test():
|
||||
|
||||
@Noparallel()
|
||||
def count(self, num=5):
|
||||
for i in range(num):
|
||||
print self, i
|
||||
time.sleep(1)
|
||||
return "%s return:%s" % (self, i)
|
||||
|
||||
class TestNoblock():
|
||||
|
||||
@Noparallel(blocking=False)
|
||||
def count(self, num=5):
|
||||
for i in range(num):
|
||||
print self, i
|
||||
time.sleep(1)
|
||||
return "%s return:%s" % (self, i)
|
||||
|
||||
def testBlocking():
|
||||
test = Test()
|
||||
test2 = Test()
|
||||
print "Counting..."
|
||||
print "Creating class1/thread1"
|
||||
thread1 = gevent.spawn(test.count)
|
||||
print "Creating class1/thread2 (ignored)"
|
||||
thread2 = gevent.spawn(test.count)
|
||||
print "Creating class2/thread3"
|
||||
thread3 = gevent.spawn(test2.count)
|
||||
|
||||
print "Joining class1/thread1"
|
||||
thread1.join()
|
||||
print "Joining class1/thread2"
|
||||
thread2.join()
|
||||
print "Joining class2/thread3"
|
||||
thread3.join()
|
||||
|
||||
print "Creating class1/thread4 (its finished, allowed again)"
|
||||
thread4 = gevent.spawn(test.count)
|
||||
print "Joining thread4"
|
||||
thread4.join()
|
||||
|
||||
print thread1.value, thread2.value, thread3.value, thread4.value
|
||||
print "Done."
|
||||
|
||||
def testNoblocking():
|
||||
test = TestNoblock()
|
||||
test2 = TestNoblock()
|
||||
print "Creating class1/thread1"
|
||||
thread1 = test.count()
|
||||
print "Creating class1/thread2 (ignored)"
|
||||
thread2 = test.count()
|
||||
print "Creating class2/thread3"
|
||||
thread3 = test2.count()
|
||||
print "Joining class1/thread1"
|
||||
thread1.join()
|
||||
print "Joining class1/thread2"
|
||||
thread2.join()
|
||||
print "Joining class2/thread3"
|
||||
thread3.join()
|
||||
|
||||
print "Creating class1/thread4 (its finished, allowed again)"
|
||||
thread4 = test.count()
|
||||
print "Joining thread4"
|
||||
thread4.join()
|
||||
|
||||
print thread1.value, thread2.value, thread3.value, thread4.value
|
||||
print "Done."
|
||||
|
||||
def testBenchmark():
|
||||
import time
|
||||
|
||||
def printThreadNum():
|
||||
import gc
|
||||
from greenlet import greenlet
|
||||
objs = [obj for obj in gc.get_objects() if isinstance(obj, greenlet)]
|
||||
print "Greenlets: %s" % len(objs)
|
||||
|
||||
printThreadNum()
|
||||
test = TestNoblock()
|
||||
s = time.time()
|
||||
for i in range(3):
|
||||
gevent.spawn(test.count, i + 1)
|
||||
print "Created in %.3fs" % (time.time() - s)
|
||||
printThreadNum()
|
||||
time.sleep(5)
|
||||
from gevent import monkey
|
||||
monkey.patch_all()
|
||||
|
||||
|
|
|
@ -11,8 +11,8 @@ queue_db = {} # Commands queued to run
|
|||
# Return: None
|
||||
|
||||
|
||||
def called(event):
|
||||
called_db[event] = time.time()
|
||||
def called(event, penalty=0):
|
||||
called_db[event] = time.time() + penalty
|
||||
|
||||
|
||||
# Check if calling event is allowed
|
||||
|
@ -62,15 +62,15 @@ def callAsync(event, allowed_again=10, func=None, *args, **kwargs):
|
|||
def call(event, allowed_again=10, func=None, *args, **kwargs):
|
||||
if isAllowed(event): # Not called recently, call it now
|
||||
called(event)
|
||||
# print "Calling now"
|
||||
# print "Calling now", allowed_again
|
||||
return func(*args, **kwargs)
|
||||
|
||||
else: # Called recently, schedule it for later
|
||||
time_left = max(0, allowed_again - (time.time() - called_db[event]))
|
||||
# print "Time left: %s" % time_left, args, kwargs
|
||||
log.debug("Calling sync (%.2fs left): %s" % (time_left, event))
|
||||
called(event, time_left)
|
||||
time.sleep(time_left)
|
||||
called(event)
|
||||
back = func(*args, **kwargs)
|
||||
if event in called_db:
|
||||
del called_db[event]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue