Coder Social home page Coder Social logo

Comments (4)

alex-sherman avatar alex-sherman commented on May 12, 2024

Functionality like that is usually left up to the application. I found a nice example on SO which I've adapted below into a handy decorator (that works with unsync).

asyncio.set_event_loop(unsync.loop)

def limit(n):
    def wrapper_gen(func):
        semaphore = asyncio.Semaphore(n)
        @wraps(func)
        async def limiter(*args, **kwargs):
            async with semaphore:
                return await func(*args, **kwargs)
        return limiter
    return wrapper_gen

@unsync
@limit(2)
async def request():
    await asyncio.sleep(1)
    print("Done")
    return "OK"

requests = [request() for i in range(10)]
print([request.result() for request in requests])

Something like this can be done with or without unsync, but IMO it's a bit easier with the single event loop. Thanks for the question!

from unsync.

yan-hic avatar yan-hic commented on May 12, 2024

@alex-sherman Could you please reopen ?
The decorator doesn't work anymore in Py 3.10 as Semaphore's loop has been deprecated.
But mainly, it doesn't work with a sync function returning something e.g. the json response from requests.get(). I think that's what the OP wanted to know.

One could (try to) implement aiohttp but then no need for unsync I believe.

from unsync.

alex-sherman avatar alex-sherman commented on May 12, 2024

I'm not sure what I could do with reopening this, my point in closing it was that it can be accomplished outside of unsync. The solution I gave would work with any normal async method.

Since the deprecation of the loop parameter you can just use the unsync event loop with asyncio.set_event_loop(unsync.loop). The request method can also return a value. I updated my solution with these changes.

from unsync.

yan-hic avatar yan-hic commented on May 12, 2024

Thx @alex-sherman.
Semaphore didn't work out for me. I ended up using the backoff library - together with unsync - as the API I was targeting returned a specific message when over-the-limit. Not really what the OP was after as it doesn't limit the rate per se and rather retry but worked for my use case.

from unsync.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.