Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async method call the sync code, and using semaphore together #2285

Closed
colinguozizhong opened this issue Aug 23, 2023 · 2 comments
Closed

async method call the sync code, and using semaphore together #2285

colinguozizhong opened this issue Aug 23, 2023 · 2 comments

Comments

@colinguozizhong
Copy link

I saw this code in model_worker.py & multi_model_worker.py

@app.post("/worker_generate")
async def api_generate(request: Request):
    params = await request.json()
    await acquire_worker_semaphore()
    worker = worker_map[params["model"]]
    output = worker.generate_gate(params)
    release_worker_semaphore()
    return JSONResponse(output)

In this asynchronous method, call the synchronous code worker.generate_gate(params), it seems to cause service congestion. In this case, await acquire_worker_semaphore() seems to be invalid because there is always only one model running in the service?

is this right?

@merrymercy
Copy link
Member

the semaphore is also used for other endpoint such as worker_generate_stream

@lvxuan263
Copy link
Contributor

lvxuan263 commented Oct 10, 2023

@merrymercy worker.generate_gate(params) is runnning in main thread. The program is not blocked by semaphore. StreamResponse will start new subthread, so /worker_generate_stream doesn't blocked.

example:
first request: POST /worker_generate , Try to resp for as long as possible
second request: POST /worker_get_status, When the first request is not completed, the second request will be blocked.

solution:
I don't know much about python. Is it elegant in this way? Place generate_gate in non main thread

output = await asyncio.to_thread(worker.generate_gate, params)

thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants