RazerM / ratelimiter

Simple Python module providing rate limiting
Apache License 2.0
120 stars 29 forks source link

Fix warning that coroutine decorator is deprecated since Python 3.8 #10

Open dhimmel opened 4 years ago

dhimmel commented 4 years ago

Our project currently uses ratelimiter-1.2.0.post0, the latest version on PyPI.

Python 3.8 has been released and we're adding a py28 CI test for our project, which resulted in the following warning:

  /home/travis/virtualenv/python3.8.0/lib/python3.8/site-packages/ratelimiter.py:127: DeprecationWarning: "@coroutine" decorator is deprecated since Python 3.8, use "async def" instead
    __aexit__ = asyncio.coroutine(__exit__)

While the master branch has unreleased commits that weren't in the version we're testing, I believe the warning would still occur on the latest master commit at the following line:

https://github.com/RazerM/ratelimiter/blob/da78a45867c3a204083c6ea8ee74f6b3e78ef524/ratelimiter/_async.py#L35

Is it possible to update the code to no longer create the warning and release a new version to PyPI with the update?

dhimmel commented 4 years ago

Ping @RazerM

RazerM commented 4 years ago

Can you submit a PR?

dhimmel commented 4 years ago

Can you submit a PR?

I read a bit about the difference between @asyncio.coroutine and async def. But I'm not familiar enough with Python's async to confidently fix the warning.

kapilt commented 4 years ago

i'm running into the same issue. it looks like theres other issues with the async implementation here, its protecting async.Lock initialization with a the synchronous thread lock, and at the moment for exit it just calls through to baseclass exit, which is just using the thread lock not the async lock. putting an async coroutine around a blocking call doesn't make it not blocking

kapilt commented 4 years ago

Can you submit a PR?

I read a bit about the difference between @asyncio.coroutine and async def. But I'm not familiar enough with Python's async to confidently fix the warning.

fwiw, in the context of the usage here the delta really isn't very material afaics.

musicinmybrain commented 2 years ago

The deprecated asyncio.coroutine was scheduled for removal in Python 3.10, but that didn’t end up happening. However, it is removed in current development versions of Python 3.11, so this still needs to be dealt with.

MilesCranmer commented 1 year ago

FYI this was officially removed in Python 3.11 so dependent packages are now breaking. @RazerM

brainysmurf commented 1 year ago

Also seeing this. If this isn't going to be fixed is there an alternative?

MilesCranmer commented 1 year ago

Snakemake might switch to throttler instead: https://github.com/snakemake/snakemake/pull/1958

uburuntu commented 1 year ago

Hello folks, it's kinda self-promoting, but since this repo was not updated since 2018 consider using https://github.com/uburuntu/throttler for async cases -- it has pretty close syntax and is a bit more efficient.

Migration for context managers

from ratelimiter import RateLimiter

rate_limiter = RateLimiter(max_calls=10, period=1)
with rate_limiter:
    do_something()

from throttler import Throttler

rate_limiter = Throttler(rate_limit=10, period=1)
async with rate_limiter:
    await do_something()

https://github.com/uburuntu/throttler#simple-example

Migration for decorators

from ratelimiter import RateLimiter

@RateLimiter(max_calls=10, period=1)
def do_something():
    pass

from throttler import throttle

@throttle(rate_limit=10, period=1)
async def do_something():
    pass

https://github.com/uburuntu/throttler#throttler-and-throttlersimultaneous

Migration example

https://github.com/snakemake/snakemake/pull/1958

RazerM commented 1 year ago

@uburuntu your project is already mentioned in this issue and in the other PR which I think is more than enough.