Exemple #1
0
def client_loop(map):
    read = asyncore.read
    write = asyncore.write
    _exception = asyncore._exception

    while map:
        try:

            # The next two lines intentionally don't use
            # iterators. Other threads can close dispatchers, causeing
            # the socket map to shrink.
            r = e = map.keys()
            w = [fd for (fd, obj) in map.items() if obj.writable()]

            try:
                r, w, e = select.select(r, w, e, client_timeout())
            except (select.error, RuntimeError) as err:
                # Python >= 3.3 makes select.error an alias of OSError,
                # which is not subscriptable but does have the 'errno' attribute
                err_errno = getattr(err, 'errno', None) or err[0]
                if err_errno != errno.EINTR:
                    if err_errno == errno.EBADF:

                        # If a connection is closed while we are
                        # calling select on it, we can get a bad
                        # file-descriptor error.  We'll check for this
                        # case by looking for entries in r and w that
                        # are not in the socket map.

                        if [fd for fd in r if fd not in map]:
                            continue
                        if [fd for fd in w if fd not in map]:
                            continue

                        # Hm, on Mac OS X, we could get a run time
                        # error and end up here, but retrying select
                        # would work.  Let's try:
                        select.select(r, w, e, 0)
                        # we survived, keep going :)
                        continue

                    raise
                else:
                    continue

            if not map:
                break

            if not (r or w or e):
                # The line intentionally doesn't use iterators. Other
                # threads can close dispatchers, causeing the socket
                # map to shrink.
                for obj in map.values():
                    if isinstance(obj, ManagedClientConnection):
                        # Send a heartbeat message as a reply to a
                        # non-existent message id.
                        try:
                            obj.send_reply(-1, None)
                        except DisconnectedError:
                            pass
                continue

            for fd in r:
                obj = map.get(fd)
                if obj is None:
                    continue
                read(obj)

            for fd in w:
                obj = map.get(fd)
                if obj is None:
                    continue
                write(obj)

            for fd in e:
                obj = map.get(fd)
                if obj is None:
                    continue
                _exception(obj)

        except:
            if map:
                try:
                    logging.getLogger(__name__ + '.client_loop').critical(
                        'A ZEO client loop failed.', exc_info=sys.exc_info())
                except:

                    pass

                for fd, obj in map.items():
                    if not hasattr(obj, 'mgr'):
                        continue
                    try:
                        obj.mgr.client.close()
                    except:
                        map.pop(fd, None)
                        try:
                            logging.getLogger(
                                __name__ + '.client_loop').critical(
                                    "Couldn't close a dispatcher.",
                                    exc_info=sys.exc_info())
                        except:
                            pass
Exemple #2
0
def client_loop(map):
    read = asyncore.read
    write = asyncore.write
    _exception = asyncore._exception

    while map:
        try:

            # The next two lines intentionally don't use
            # iterators. Other threads can close dispatchers, causeing
            # the socket map to shrink.
            r = e = map.keys()
            w = [fd for (fd, obj) in map.items() if obj.writable()]

            try:
                r, w, e = select.select(r, w, e, client_timeout())
            except select.error as err:
                if err[0] != errno.EINTR:
                    if err[0] == errno.EBADF:

                        # If a connection is closed while we are
                        # calling select on it, we can get a bad
                        # file-descriptor error.  We'll check for this
                        # case by looking for entries in r and w that
                        # are not in the socket map.

                        if [fd for fd in r if fd not in map]:
                            continue
                        if [fd for fd in w if fd not in map]:
                            continue

                    raise
                else:
                    continue

            if not map:
                break

            if not (r or w or e):
                # The line intentionally doesn't use iterators. Other
                # threads can close dispatchers, causeing the socket
                # map to shrink.
                for obj in map.values():
                    if isinstance(obj, ManagedClientConnection):
                        # Send a heartbeat message as a reply to a
                        # non-existent message id.
                        try:
                            obj.send_reply(-1, None)
                        except DisconnectedError:
                            pass
                continue

            for fd in r:
                obj = map.get(fd)
                if obj is None:
                    continue
                read(obj)

            for fd in w:
                obj = map.get(fd)
                if obj is None:
                    continue
                write(obj)

            for fd in e:
                obj = map.get(fd)
                if obj is None:
                    continue
                _exception(obj)

        except:
            if map:
                try:
                    logging.getLogger(__name__ + '.client_loop').critical(
                        'A ZEO client loop failed.', exc_info=sys.exc_info())
                except:

                    pass

                for fd, obj in map.items():
                    if not hasattr(obj, 'mgr'):
                        continue
                    try:
                        obj.mgr.client.close()
                    except:
                        map.pop(fd, None)
                        try:
                            logging.getLogger(
                                __name__ + '.client_loop').critical(
                                    "Couldn't close a dispatcher.",
                                    exc_info=sys.exc_info())
                        except:
                            pass
Exemple #3
0
def client_loop(map):
    read = asyncore.read
    write = asyncore.write
    _exception = asyncore._exception

    while map:
        try:

            # The next two lines intentionally don't use
            # iterators. Other threads can close dispatchers, causeing
            # the socket map to shrink.
            r = e = map.keys()
            w = [fd for (fd, obj) in map.items() if obj.writable()]

            try:
                r, w, e = select.select(r, w, e, client_timeout())
            except (select.error, RuntimeError) as err:
                # Python >= 3.3 makes select.error an alias of OSError,
                # which is not subscriptable but does have the 'errno' attribute
                err_errno = getattr(err, "errno", None) or err[0]
                if err_errno != errno.EINTR:
                    if err_errno == errno.EBADF:

                        # If a connection is closed while we are
                        # calling select on it, we can get a bad
                        # file-descriptor error.  We'll check for this
                        # case by looking for entries in r and w that
                        # are not in the socket map.

                        if [fd for fd in r if fd not in map]:
                            continue
                        if [fd for fd in w if fd not in map]:
                            continue

                        # Hm, on Mac OS X, we could get a run time
                        # error and end up here, but retrying select
                        # would work.  Let's try:
                        select.select(r, w, e, 0)
                        # we survived, keep going :)
                        continue

                    raise
                else:
                    continue

            if not map:
                break

            if not (r or w or e):
                # The line intentionally doesn't use iterators. Other
                # threads can close dispatchers, causeing the socket
                # map to shrink.
                for obj in map.values():
                    if isinstance(obj, ManagedClientConnection):
                        # Send a heartbeat message as a reply to a
                        # non-existent message id.
                        try:
                            obj.send_reply(-1, None)
                        except DisconnectedError:
                            pass
                continue

            for fd in r:
                obj = map.get(fd)
                if obj is None:
                    continue
                read(obj)

            for fd in w:
                obj = map.get(fd)
                if obj is None:
                    continue
                write(obj)

            for fd in e:
                obj = map.get(fd)
                if obj is None:
                    continue
                _exception(obj)

        except:
            if map:
                try:
                    logging.getLogger(__name__ + ".client_loop").critical(
                        "A ZEO client loop failed.", exc_info=sys.exc_info()
                    )
                except:

                    pass

                for fd, obj in map.items():
                    if not hasattr(obj, "mgr"):
                        continue
                    try:
                        obj.mgr.client.close()
                    except:
                        map.pop(fd, None)
                        try:
                            logging.getLogger(__name__ + ".client_loop").critical(
                                "Couldn't close a dispatcher.", exc_info=sys.exc_info()
                            )
                        except:
                            pass
def client_loop(map):
    read = asyncore.read
    write = asyncore.write
    _exception = asyncore._exception

    while map:
        try:

            # The next two lines intentionally don't use
            # iterators. Other threads can close dispatchers, causeing
            # the socket map to shrink.
            r = e = map.keys()
            w = [fd for (fd, obj) in map.items() if obj.writable()]

            try:
                r, w, e = select.select(r, w, e, client_timeout())
            except select.error as err:
                if err[0] != errno.EINTR:
                    if err[0] == errno.EBADF:

                        # If a connection is closed while we are
                        # calling select on it, we can get a bad
                        # file-descriptor error.  We'll check for this
                        # case by looking for entries in r and w that
                        # are not in the socket map.

                        if [fd for fd in r if fd not in map]:
                            continue
                        if [fd for fd in w if fd not in map]:
                            continue

                    raise
                else:
                    continue

            if not map:
                break

            if not (r or w or e):
                # The line intentionally doesn't use iterators. Other
                # threads can close dispatchers, causeing the socket
                # map to shrink.
                for obj in map.values():
                    if isinstance(obj, ManagedClientConnection):
                        # Send a heartbeat message as a reply to a
                        # non-existent message id.
                        try:
                            obj.send_reply(-1, None)
                        except DisconnectedError:
                            pass
                continue

            for fd in r:
                obj = map.get(fd)
                if obj is None:
                    continue
                read(obj)

            for fd in w:
                obj = map.get(fd)
                if obj is None:
                    continue
                write(obj)

            for fd in e:
                obj = map.get(fd)
                if obj is None:
                    continue
                _exception(obj)

        except:
            if map:
                try:
                    logging.getLogger(__name__+'.client_loop').critical(
                        'A ZEO client loop failed.',
                        exc_info=sys.exc_info())
                except:

                    pass

                for fd, obj in map.items():
                    if not hasattr(obj, 'mgr'):
                        continue
                    try:
                        obj.mgr.client.close()
                    except:
                        map.pop(fd, None)
                        try:
                            logging.getLogger(__name__+'.client_loop'
                                              ).critical(
                                "Couldn't close a dispatcher.",
                                exc_info=sys.exc_info())
                        except:
                            pass