Skip to content

Sync mode no longer works when asyncio event loop is running #893

@jacobtomlinson

Description

@jacobtomlinson

Thanks for all the hard work here, really excited to see the asyncio support released.

However I've noticed a bug that the original threaded mode no longer works if the asyncio event loop is running.

import asyncio

from zeroconf import IPVersion, ServiceInfo, Zeroconf


def get_addresses_for_service(service: str):
    with Zeroconf(ip_version=IPVersion.V4Only) as zeroconf:
        service = zeroconf.get_service_info("_http._tcp.local.", service, 3000)
        for addr in service.parsed_addresses():
            print(f"{addr}:{service.port}")


async def run():
    get_addresses_for_service("_mysvc._http._tcp.local.")


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())
Traceback (most recent call last):
  File "/tmp/test.py", line 28, in <module>
    loop.run_until_complete(run())
  File "/Users/jtomlinson/miniconda3/envs/dask/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/tmp/test.py", line 22, in run
    get_addresses_for_service("_sched-7bca6959._dask._tcp.local.")
  File "/tmp/test.py", line 9, in get_addresses_for_service
    service = zeroconf.get_service_info("_dask._tcp.local.", service, 3000)
  File "/Users/jtomlinson/Projects/jstasiak/python-zeroconf/zeroconf/_core.py", line 449, in get_service_info
    if info.request(self, timeout, question_type):
  File "/Users/jtomlinson/Projects/jstasiak/python-zeroconf/zeroconf/_services/info.py", line 427, in request
    raise RuntimeError("Use AsyncServiceInfo.async_request from the event loop")
RuntimeError: Use AsyncServiceInfo.async_request from the event loop

Despite creating a regular Zeroconf object within a synchronous function I'm getting an exception saying that I need to use async functionality. This works as expected if I downgrade to 0.31.0.

To work around this I could change the synchronous function to be async and use the new functionality, but this would break the library API for users which I would rather avoid.

OS: macOS 10.15.7
Python: 3.8
Zeroconf: 0.32.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions