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

WIP: async awesomeness #3190

Closed
wants to merge 1 commit into from
Closed

WIP: async awesomeness #3190

wants to merge 1 commit into from

Conversation

bbangert
Copy link
Member

@bbangert bbangert commented Sep 4, 2016

Description:

WIP: NOT READY FOR MERGE YET

This replaces the primary mechanism by which HASS runs.

The existing architecture exposes 3 core.py API objects, EventBus, StateMachine, and ServiceRegistry. These are called from a variety of threads and are all blocking calls that attempt to acquire per-object locks to fully execute.

This change retains the existing thread-safe blocking API for use by all components, as well as the blocking nature of component setup. However, it starts up an asyncio event loop on start, which is where the actual work is done. The thread-safe API calls schedule the async API variant version into the event loop, then blocks waiting until it has run to completion.

EventBus registrations that are marked as coroutines run on the event loop and may directly run async versions of the appropriate API's. To retain similar call characteristics as before, EventBus's async_fire schedules coroutines to run later on the event loop while sending non-coroutines to the thread-pool as before. This does mean that coroutine listeners are guaranteed to run after all listeners were located, while non-coroutines may start executing immediately in the other thread.

This WIP uses Python 3.4.4 functionality as a proving ground for the approach, using earlier 3.4 should be fine with a backported function or two for asyncio features needed (such as run_coroutine_threadsafe).

@@ -1,5 +1,6 @@
"""Provides methods to bootstrap a home assistant instance."""

import asyncio

Choose a reason for hiding this comment

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

farcy v1.1

  • 1: F401 'asyncio' imported but unused

@balloob
Copy link
Member

balloob commented Sep 7, 2016

I really like this and I think this will be great change.

I looked at the first failing test, tests/test_bootstrap.py::TestBootstrap::test_platform_specific_config_validation and that seems to be because we pass the wrong arguments to MockPlatform on line 283. It should be MockPlatform(platform_schema=platform_schema)). However, that seems to fail while waiting for the result of setting up listening for a discovery:

Traceback (most recent call last):
  File "/Users/paulus/dev/python/home-assistant/homeassistant/bootstrap.py", line 102, in _setup_component
    result = component.setup(hass, config)
  File "/Users/paulus/dev/python/home-assistant/homeassistant/components/switch/__init__.py", line 76, in setup
    component.setup(config)
  File "/Users/paulus/dev/python/home-assistant/homeassistant/helpers/entity_component.py", line 63, in setup
    component_platform_discovered)
  File "/Users/paulus/dev/python/home-assistant/homeassistant/helpers/discovery.py", line 60, in listen_platform
    hass.bus.listen(EVENT_PLATFORM_DISCOVERED, discovery_platform_listener)
  File "/Users/paulus/dev/python/home-assistant/homeassistant/core.py", line 340, in listen
    future.result()
[…snip…]

I'm down to help with fixing the tests and the other issues. Shall we move the branch to the HA repo so we can both push to it?

@balloob
Copy link
Member

balloob commented Sep 7, 2016

btw, I have added the fix for the bootstrap test to #3196 as it contained some other misc fixes.

@balloob balloob closed this Sep 7, 2016
@home-assistant home-assistant locked and limited conversation to collaborators Mar 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants