(event, allowed_again=10, func=None, *args, **kwargs)
| 46 | # If the function called again within the rate limit interval then previous queued call will be dropped |
| 47 | # Return: Immediately gevent thread |
| 48 | def callAsync(event, allowed_again=10, func=None, *args, **kwargs): |
| 49 | if isAllowed(event, allowed_again): # Not called recently, call it now |
| 50 | called(event) |
| 51 | # print "Calling now" |
| 52 | return gevent.spawn(func, *args, **kwargs) |
| 53 | else: # Called recently, schedule it for later |
| 54 | time_left = allowed_again - max(0, time.time() - called_db[event]) |
| 55 | log.debug("Added to queue (%.2fs left): %s " % (time_left, event)) |
| 56 | if not queue_db.get(event): # Function call not queued yet |
| 57 | thread = gevent.spawn_later(time_left, lambda: callQueue(event)) # Call this function later |
| 58 | queue_db[event] = (func, args, kwargs, thread) |
| 59 | return thread |
| 60 | else: # Function call already queued, just update the parameters |
| 61 | thread = queue_db[event][3] |
| 62 | queue_db[event] = (func, args, kwargs, thread) |
| 63 | return thread |
| 64 | |
| 65 | |
| 66 | # Rate limit and delay function call if needed |
no test coverage detected