Skip to content

Rewritten library using asyncio#1048

Open
albaintor wants to merge 11 commits intohome-assistant-libs:masterfrom
albaintor:master
Open

Rewritten library using asyncio#1048
albaintor wants to merge 11 commits intohome-assistant-libs:masterfrom
albaintor:master

Conversation

@albaintor
Copy link
Copy Markdown

Hi,

I rewrote the library using asyncio instead of threads. I had issues with the old behaviour and it was not suitable to use with an async program.

I tested it successfully on my side. Of course there are some breaking changes :
Instead of calling wait method, we use the connect method directly like this :

chromecast=pychromecast.get_chromecast_from_host((host, 8009, None, None, None))
await chromecast.connect(timeout=5)

As you can see the code is much more simpler as a lot of work is performed directly in standard wrappers of asyncio.

@elupus elupus closed this Apr 11, 2025
@elupus elupus reopened this Apr 11, 2025
@elupus
Copy link
Copy Markdown
Collaborator

elupus commented Apr 11, 2025

Sorry pressed wrong button. I like the effort, but i wonder how we can handle this in a non breaking way for current users of the library.

@albaintor
Copy link
Copy Markdown
Author

albaintor commented Apr 11, 2025

There is indeed some breaking changes but not so much, only those 2 :

  • the initialization of the connection should be replaced with an await prefix in an async method
  • all the calls to commands should be prefixed with await in an async method

The rest remains unchanged, including all the registrations to the listeners.

This is a great benefit I think especially for HA which works only with a single event loop in a single thread.
I experienced random deadlocks with the current implementation with events / threading.

Also the new implementation is more efficient in performances / CPU usage.

Maybe you could create a forked branch (with dedicated releases, eg beta 15.0) in your repository that could live in parallel, awaiting for users to migrate to the new implementation.

@elupus
Copy link
Copy Markdown
Collaborator

elupus commented Sep 14, 2025

I just wanted this to get better support for happyeyeballs connection of sockets. Are you still interested in trying to get this further?

# first 4 bytes is Big-Endian payload length
read_len = struct.unpack(">I", data[0:4])[0]
data = data[4:]
data = data[:read_len]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

There is no guarantee this data is the full payload. It could haven been split in chunks. So this need to accumulate as much as it need.

I think a standard asyncio task with just recv calls would be easier. It would also reduce the rather huge diff on this file.

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