Skip to content Skip to sidebar Skip to footer

Signal Handling In Pylons

I have a pylons project where I need to update some in-memory structures periodically. This should be done on-demand. I decided to come up with a signal handler for this. User send

Solution 1:

Yes, it is possible, but not easy using the stock Python libraries. This is due to Python translating all OS errors to exceptions. However, EINTR should really cause a retry of the system call used. Whenever you start using signals in Python you will see this error sporadically.

I have code that fixes this (SafeSocket), by forking Python modules and adding that functionality. But it needs to be added everywhere system calls are used. So it's possible, but not easy. But you can use my open-source code, it may save you years of work. ;-)

The basic pattern is this (implemented as a system call decorator):

# decorator to make system call methods safe from EINTRdefsystemcall(meth):
    # have to import this way to avoid a circular importfrom _socket import error as SocketError
    defsystemcallmeth(*args, **kwargs):
        while1:
            try:
                    rv = meth(*args, **kwargs)
            except EnvironmentError as why:
                if why.args and why.args[0] == EINTR:
                    continueelse:
                    raiseexcept SocketError as why:
                if why.args and why.args[0] == EINTR:
                    continueelse:
                    raiseelse:
                breakreturn rv
    return systemcallmeth

You could also just use that around your select call.

Solution 2:

A fix, at least works for me, from an 12 year old python-dev list post

whileTrue:
        try:
            readable, writable, exceptional = select.select(inputs, outputs, inputs, timeout)
        except select.error, v:
            if v[0] != errno.EINTR: raise
        else: break

The details of the actual select line isn't important... your "fd_sets = select.select([self], [], [], timeout)" line should work exactly the same.

The important bit is to check for EINTR and retry/loop if that is caught. Oh, and don't forget to import errno.

Post a Comment for "Signal Handling In Pylons"