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

Patch to fix Home Assistant Blocking Call warnings #221

Closed

Conversation

jleinenbach
Copy link
Contributor

This should solve:
#217

@jleinenbach jleinenbach changed the title Jleinenbach patch for issue 217 Patch to fix Home Assistant Blocking Call warnings May 22, 2024
Copy link
Owner

@jekalmin jekalmin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!
Could you make a change as commented?

I referenced the code from below.
https://developers.home-assistant.io/docs/asyncio_blocking_operations/#running-blocking-calls-in-the-executor

Comment on lines +167 to +173
# Define an asynchronous function that lists models with a timeout using asyncio.to_thread
async def list_models_with_timeout():
# Use asyncio.to_thread to run the blocking call in a separate thread
return await asyncio.to_thread(client.models.list, timeout=10)

# Await the execution of the list_models_with_timeout function
await list_models_with_timeout()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change this to following?

Suggested change
# Define an asynchronous function that lists models with a timeout using asyncio.to_thread
async def list_models_with_timeout():
# Use asyncio.to_thread to run the blocking call in a separate thread
return await asyncio.to_thread(client.models.list, timeout=10)
# Await the execution of the list_models_with_timeout function
await list_models_with_timeout()
await hass.async_add_executor_job(partial(client.models.list, timeout=10))

Also, importing partial is required at the top.

from functools import partial

@jekalmin
Copy link
Owner

Let me fix changes as I suggested above.
Thanks for digging into the issue!

#264

@jekalmin jekalmin closed this Nov 10, 2024
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

Successfully merging this pull request may close these issues.

2 participants