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

Run loop.run_until_complete in another thread #2701

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lmdeploy/serve/async_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,19 @@ async def gather():
await asyncio.gather(
*[_inner_call(i, generators[i]) for i in range(len(prompts))])

_get_event_loop().run_until_complete(gather())
loop = _get_event_loop()
if loop.is_running():
# when there is another event loop running in the same thread
# it will raise error. Putting it in another thread might case
# slightly performance drop, about 10%
logger.warn(
'Another event loop running detected. Run this event loop '
'in a separate thread. This might cause performance drop.')
thread = Thread(target=lambda: loop.run_until_complete(gather()))
thread.start()
thread.join()
else:
loop.run_until_complete(gather())
outputs = outputs[0] if need_list_wrap else outputs
return outputs

Expand Down Expand Up @@ -516,7 +528,7 @@ async def generate(
elif gen_config.random_seed is None and sequence_start:
gen_config.random_seed = random.getrandbits(64)
if gen_config.n > 1:
logger.ERROR(f"n({gen_config.n}) > 1 hasn't been supported yet. "
logger.error(f"n({gen_config.n}) > 1 hasn't been supported yet. "
f'Fallback to 1')
gen_config.n = 1
prompt = messages
Expand Down
Loading