Preferred way to timeout a function call ?

Posted on
Wed Aug 16, 2023 8:11 pm
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Preferred way to timeout a function call ?

I'm checking the On / Off status of a remote piece of hardware (TV). It's a direct connection to the IP, so when the TV isn't on it hangs and eventually throws an exception. It takes about 60 seconds. It works, but it takes too long.

I'd like to force a timeout after 5 seconds so other devices don't hang. What's the preferred way to do that? Here's my function call :

Code: Select all
try:
         tv = samsungtvws.SamsungTVWS(host=ipaddress, port=8002, token_file=token_file)
         info = tv.rest_device_info()
         tvState = info['device']['PowerState']
         self.logger.debug(tvState)
      except Exception as m:
         #self.logger.debug("Error when accessing device: " + str(m))
         self.logger.debug("Could be off. Skipping...")
         device.updateStateOnServer("status", "off")
         device.updateStateImageOnServer(indigo.kStateImageSel.SensorOff)
         return


Posted on
Thu Aug 17, 2023 5:56 am
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Preferred way to timeout a function call ?

I tend to use threads for such things. Start the function call in a thread and join it after X time. I don't know if that's necessarily the preferred way, but I've had good success.

https://docs.python.org/3.10/library/th ... hread.join

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Thu Aug 17, 2023 7:21 am
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Preferred way to timeout a function call ?

Looking at the code for the library you're using, there's actually a timeout parameter you could use:
Code: Select all
class SamsungTVWS(connection.SamsungTVWSConnection):
    def __init__(
        self,
        host: str,
        token: Optional[str] = None,
        token_file: Optional[str] = None,
        port: int = 8001,
        timeout: Optional[float] = None,
        key_press_delay: float = 1,
        name: str = "SamsungTvRemote",
    ) -> None:


So maybe just try:
Code: Select all
try:
         tv = samsungtvws.SamsungTVWS(host=ipaddress, port=8002, token_file=token_file, timeout=3.0)
         info = tv.rest_device_info()
         tvState = info['device']['PowerState']
         self.logger.debug(tvState)
      except Exception as m:
         #self.logger.debug("Error when accessing device: " + str(m))
         self.logger.debug("Could be off. Skipping...")
         device.updateStateOnServer("status", "off")
         device.updateStateImageOnServer(indigo.kStateImageSel.SensorOff)
         return


joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Thu Aug 17, 2023 7:24 am
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Preferred way to timeout a function call ?

Well spotted. It's too bad more libraries don't have that option.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Thu Aug 17, 2023 8:55 pm
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Preferred way to timeout a function call ?

Wow. Good lookin out. That worked perfectly. Thanks!

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 9 guests