1

I am trying to handle timeout exception when I connecting to CIM server like that:

 si = pywbem.WBEMConnection(HOST, ("root", "passwrord"), "ns",no_verification=True)

I Googled, that this function have a parameter name: "timeout" but it's only for newer version PyWBEM, which I cannot be using unfortunately.

The error I get after 5 minutes:

pywbem.cim_operations.CIMError: (0, 'Socket error: [Errno 110] Connection timed out')

I would like to set this interval to 30 second. How should I do this? I am accepting every kind of solution. I'm thinking of a while loop, which counts down for 30 seconds, but I couldn't figure out how to examine the connection is established or not.

Thanks

PyVas
  • 49
  • 1
  • 6

1 Answers1

0

Hmmm... I imagine you would have to create some type of asynchronous process to do the check.

Borrowing from a solution from here by Rich, here is what I would try:

import multiprocessing.pool
import functools

def timeout(max_timeout):
    """Timeout decorator, parameter in seconds."""
    def timeout_decorator(item):
        """Wrap the original function."""
        @functools.wraps(item)
        def func_wrapper(*args, **kwargs):
            """Closure for function."""
            pool = multiprocessing.pool.ThreadPool(processes=1)
            async_result = pool.apply_async(item, args, kwargs)
            # raises a TimeoutError if execution exceeds max_timeout
            return async_result.get(max_timeout)
        return func_wrapper
    return timeout_decorator

@timeout(30) # timeout after 30 seconds
def connect():
    return pywbem.WBEMConnection(HOST, ("root", "passwrord"), "ns",no_verification=True)

try:
    si = connect()
except multiprocessing.TimeoutError:
    si = "Connection timed out after 30 seconds"

print si
Community
  • 1
  • 1
Scratch'N'Purr
  • 9,959
  • 2
  • 35
  • 51