You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importzeroconfinstance=zeroconf.Zeroconf()
browser=zeroconf.ServiceBrowser(
instance,
['_osc._udp.local.'],
handlers=[handler]
)
defhandler(zeroconf, service_type, name, state_change):
# I cannot use zeroconf.ServiceStateChange.Added here, because zeroconf is not the import but the argumentpass
we have the problem that handler() is receiving a named argument zeroconf that overloads import zeroconf and that we cannot change. If I defined
Just for completeness sake, this can easily be extended to support async callbacks, which prevents incorrect usage of ensure_future()/create_task() demonstrated in all the examples:
importasynciofromfunctoolsimportwraps_zeroconf_pending_handler_tasks=set()
defasync_zeroconf_handler(handler):
# `wrapper`'s argument names are important!@wraps(handler)defwrapper(zeroconf, service_type, name, state_change):
# Keep a reference to the task around until it's done, to prevent it from being garbage collected.task=asyncio.create_task(handler(zeroconf, service_type, name, state_change))
_zeroconf_pending_handler_tasks.add(task)
task.add_done_callback(_zeroconf_pending_handler_tasks.discard)
returnwrapper# ...@async_zeroconf_handlerasyncdefhandler(zc, type_, name, new_state):
awaitasyncio.sleep(1)
browser=zeroconf.ServiceBrowser(
instance,
['_osc._udp.local.'],
handlers=[handler]
)
In the default pattern
we have the problem that
handler()
is receiving a named argumentzeroconf
that overloadsimport zeroconf
and that we cannot change. If I definedI would receive
To avoid it I would have to alias the import, which is a bit ugly:
It would be nice if the arguments were to be passed in as positional arguments, or named differently, so that the names didn't clash.
The text was updated successfully, but these errors were encountered: