Comments (4)
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.
@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.
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.
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)
- Async generators HOT 2
- Defining the amount of threads in use for sync functions HOT 3
- How to handle errors and exceptions? HOT 1
- Can't Import unsync on AWS Lambda HOT 5
- Cython support
- @unsync(cpu_bound=True) Windows 10 64 error HOT 1
- Any support or alternative for `asyncio.Semaphore`? HOT 4
- Calling nested results() on futures produces InvalidStateError HOT 1
- Unsync conda-forge recipe
- Python 3.9.x compatability? HOT 6
- Decorator problem HOT 1
- process_executor.py example on Windows 7 orphans the multiprocess kernels HOT 1
- Leftover debug? DERP HOT 2
- Continuation with cpu_bound functions HOT 2
- CPU Bound Decorator in Wrapped Functions HOT 1
- Using @unsync decorator in Jupyter results in error HOT 1
- max_workers HOT 3
- closing old processes HOT 3
- `BrokenProcessPool` error using function with `cpu_bound=True` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from unsync.