Created
November 22, 2020 12:15
-
-
Save udnaan/0dabcc80205753436649ce40a411bfa5 to your computer and use it in GitHub Desktop.
gevent rate limiter pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gevent | |
processes = {} | |
def process(id, data): | |
print(f'{id=} {data=}') | |
# reschedule the process function to run after a second. | |
# If reschedulre is called again before 1 second is up, the previous function is cancelled | |
def reschedule(id, data): | |
if processes.get(id, None): | |
processes[id].kill() | |
processes[id] = gevent.spawn_later(1, process, id, data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment