Skip to content

Instantly share code, notes, and snippets.

@udnaan
Created November 22, 2020 12:15
Show Gist options
  • Save udnaan/0dabcc80205753436649ce40a411bfa5 to your computer and use it in GitHub Desktop.
Save udnaan/0dabcc80205753436649ce40a411bfa5 to your computer and use it in GitHub Desktop.
gevent rate limiter pattern
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