appdaemon icon indicating copy to clipboard operation
appdaemon copied to clipboard

Configure timeout for calling services

Open lukaszwawrzyk opened this issue 1 year ago • 8 comments

Is there an existing feature request for this?

  • [X] I have searched the existing issues

Your feature request

I am calling a service that uses TTS and I want to wait till it is done speaking before invoking another action. Sometimes there is a lot to say and I get a timeout error. This causes that the message doesn't finish and is interrupted by another invocation that happened after the timeout. I think I am hitting the log message from here (hassplugin.py:536):

self.logger.warning(
                "Timeout in call_service(%s/%s/%s, %s)", namespace, domain, service, data,
            )

Is there a way to increase the timeout? I already set thread_duration_warning_threshold which removed some warnings, but this behavior is a problem for me.

lukaszwawrzyk avatar Aug 07 '23 20:08 lukaszwawrzyk

In general you shouldn't be holding up threads for anything, you need to find a different way. I solved the same problem by creating my own worker thread and waiting for the status of the media player to change to idle. The thread has it's own Q so everything is processed in order, and the actually callback only has to add something to the Q which happens very quickly.

acockburn avatar Aug 07 '23 20:08 acockburn

I don't think I should be holding a thread if I await for response from home assistant that is just long. It should be async waiting, right?

there is:

r = await self.session.post(api_url, headers=headers, json=data, verify_ssl=self.cert_verify)

it throws asyncio.TimeoutError. There is some arbitrary timeout of 10-20 seconds or so. I'd like it to be 60s for example.

lukaszwawrzyk avatar Aug 07 '23 20:08 lukaszwawrzyk

That depends on what your code looks like ...


From: Łukasz Wawrzyk @.> Sent: Monday, August 7, 2023 4:36 PM To: AppDaemon/appdaemon @.> Cc: Andrew Cockburn @.>; Comment @.> Subject: Re: [AppDaemon/appdaemon] Configure timeout for calling services (Issue #1849)

I don't think I should be holding a thread if I await for response from home assistant that is just long. It should be async waiting, right?

— Reply to this email directly, view it on GitHubhttps://github.com/AppDaemon/appdaemon/issues/1849#issuecomment-1668544867, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACLE7JD6LLGTDZYTHQEV3X3XUFGTPANCNFSM6AAAAAA3HPRDPQ. You are receiving this because you commented.Message ID: @.***>

acockburn avatar Aug 07 '23 20:08 acockburn

My code is simply

await self.call_service("script/notify_say", message=message, return_result=True)

I want it to wait for the result, but it is written as async code, so I believe I am not blocking threads. Just this arbitrary timeout from appdemon internals that sends http request to HA is aborting the wait.

lukaszwawrzyk avatar Aug 07 '23 20:08 lukaszwawrzyk

That's not how async works unfortunately. You aren't blocking the thread itself, correct, but you are blocking execution of the callback as is yields control during the await. This is not good practice as AppDaemon will Q up any other events that try to use that callback until it frees up. Also, at the moment, the results of setting "return_result" to true for a Home Assistant service are undefined - that is only supported for internal AppDaemon services. So:

  1. Don't set return_result=true
  2. Find another way to handle waiting. E.g. my thread proposal, or you could do a listen state on the media player and continue processing when the state returns to idle.

From: Łukasz Wawrzyk @.> Sent: Monday, August 7, 2023 4:40 PM To: AppDaemon/appdaemon @.> Cc: Andrew Cockburn @.>; Comment @.> Subject: Re: [AppDaemon/appdaemon] Configure timeout for calling services (Issue #1849)

My code is simply

await self.call_service("script/notify_say", message=message, return_result=True)

I want it to wait for the result, but it is written as async code, so I believe I am not blocking threads. Just this arbitrary timeout from appdemon internals that sends http request to HA is aborting the wait.

— Reply to this email directly, view it on GitHubhttps://github.com/AppDaemon/appdaemon/issues/1849#issuecomment-1668549695, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACLE7JBDMI4HFM7ITUASGHLXUFHEHANCNFSM6AAAAAA3HPRDPQ. You are receiving this because you commented.Message ID: @.***>

acockburn avatar Aug 07 '23 20:08 acockburn

Can you point me to some resources where I can read about this async behavior that you mentioned?

lukaszwawrzyk avatar Aug 07 '23 21:08 lukaszwawrzyk

What platform is the TTS service for? If you're using Alexa, you can use a data type of "announce" which has much better behavior than TTS. If not then I don't know if announce is supported.

Justihar avatar Aug 07 '23 21:08 Justihar

@Justihar I am using nabu casa to generate voice. I also have it integrated with ytube_music so that it pauses the playback to speak and restores it after.

lukaszwawrzyk avatar Aug 07 '23 21:08 lukaszwawrzyk