示例#1
0
class EventletWorker(AsyncWorker):
    @classmethod
    def setup(cls):
        import eventlet
        if eventlet.version_info < (0, 9, 7):
            raise RuntimeError("You need eventlet >= 0.9.7")
        eventlet.monkey_patch(os=False)

    def init_process(self):
        hubs.use_hub()
        super(EventletWorker, self).init_process()

    def timeout_ctx(self):
        return eventlet.Timeout(self.cfg.keepalive, False)

    def run(self):
        self.socket = GreenSocket(family_or_realsock=self.socket.sock)
        self.socket.setblocking(1)
        self.acceptor = eventlet.spawn(eventlet.serve, self.socket,
                                       self.handle, self.worker_connections)

        while self.alive:
            self.notify()
            if self.ppid != os.getppid():
                self.log.info("Parent changed, shutting down: %s" % self)
                break

            eventlet.sleep(1.0)

        self.notify()
        with eventlet.Timeout(self.timeout, False):
            eventlet.kill(self.acceptor, eventlet.StopServe)
示例#2
0
    def run(self):
        acceptors = []
        for sock in self.sockets:
            gsock = GreenSocket(sock)
            gsock.setblocking(1)
            hfun = partial(self.handle, gsock)
            acceptor = eventlet.spawn(_eventlet_serve, gsock, hfun,
                                      self.worker_connections)

            acceptors.append(acceptor)
            eventlet.sleep(0.0)

        while self.alive:
            self.notify()
            eventlet.sleep(1.0)

        self.notify()
        try:
            with eventlet.Timeout(self.cfg.graceful_timeout) as t:
                for a in acceptors:
                    a.kill(eventlet.StopServe())
                for a in acceptors:
                    a.wait()
        except eventlet.Timeout as te:
            if te != t:
                raise
            for a in acceptors:
                a.kill()
示例#3
0
class Listen(object):
    def __init__(self):
        self.host = config.get("HOST")
        self.port = int(config.get("PORT"))
        self.backlog = int(config.get("BACKLOG"))
        self.sock = None
        hubs.use_hub()


#    def open_socket(self):
#        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#        self.sock = GreenSocket(s)
#        self.sock.bind((self.host, self.port))
#        self.sock.listen(self.backlog)

    def open_socket(self):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            #self.sock = GreenSocket(family_or_realsock=s)
            s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self.sock = GreenSocket(s)
            self.sock.setblocking(1)
            self.sock.bind((self.host, self.port))
            self.sock.listen(50)
        except socket.error:
            if self.sock:
                self.close_socket()
            sys.exit(1)

    def close_socket(self):
        if self.sock:
            self.sock.close()
        sys.exit(1)
示例#4
0
    def run(self):
        acceptors = []
        for sock in self.sockets:
            gsock = GreenSocket(sock)
            gsock.setblocking(1)
            hfun = partial(self.handle, gsock)
            acceptor = eventlet.spawn(_eventlet_serve, gsock, hfun,
                                      self.worker_connections)

            acceptors.append(acceptor)
            eventlet.sleep(0.0)

        while self.alive:
            self.notify()
            eventlet.sleep(1.0)

        self.notify()
        try:
            with eventlet.Timeout(self.cfg.graceful_timeout) as t:
                [a.kill(eventlet.StopServe()) for a in acceptors]
                [a.wait() for a in acceptors]
        except eventlet.Timeout as te:
            if te != t:
                raise
            [a.kill() for a in acceptors]
示例#5
0
class EventletWorker(AsyncWorker):

    @classmethod
    def setup(cls):
        import eventlet
        if eventlet.version_info < (0,9,7):
            raise RuntimeError("You need eventlet >= 0.9.7")
        eventlet.monkey_patch(os=False)

    def init_process(self):
        hubs.use_hub()
        super(EventletWorker, self).init_process()

    def timeout_ctx(self):
        return eventlet.Timeout(self.cfg.keepalive, False)

    def run(self):
        self.socket = GreenSocket(family_or_realsock=self.socket.sock)
        self.socket.setblocking(1)
        self.acceptor = eventlet.spawn(eventlet.serve, self.socket,
                self.handle, self.worker_connections)

        while self.alive:
            self.notify()
            if self.ppid != os.getppid():
                self.log.info("Parent changed, shutting down: %s", self)
                break

            eventlet.sleep(1.0)

        self.notify()
        with eventlet.Timeout(self.timeout, False):
            eventlet.kill(self.acceptor, eventlet.StopServe)
示例#6
0
    def run(self):
        acceptors = []
        for sock in self.sockets:
            s = GreenSocket(family_or_realsock=sock)
            s.setblocking(1)
            hfun = partial(self.handle, s)
            acceptor = eventlet.spawn(eventlet.serve, s, hfun,
                                      self.worker_connections)

            acceptors.append(acceptor)

        while self.alive:
            self.notify()
            if self.ppid != os.getppid():
                self.log.info("Parent changed, shutting down: %s", self)
                break

            eventlet.sleep(1.0)

        self.notify()
        try:
            with eventlet.Timeout(self.cfg.graceful_timeout) as t:
                [a.wait() for a in acceptors]
        except eventlet.Timeout as te:
            if te != t:
                raise
            [a.kill() for a in acceptors]
示例#7
0
    def run(self):
        acceptors = []
        for sock in self.sockets:
            s = GreenSocket(family_or_realsock=sock)
            s.setblocking(1)
            hfun = partial(self.handle, s)
            acceptor = eventlet.spawn(eventlet.serve, s, hfun,
                    self.worker_connections)

            acceptors.append(acceptor)

        while self.alive:
            self.notify()
            if self.ppid != os.getppid():
                self.log.info("Parent changed, shutting down: %s", self)
                break

            eventlet.sleep(1.0)

        self.notify()
        try:
            with eventlet.Timeout(self.cfg.graceful_timeout) as t:
                [a.wait() for a in acceptors]
        except eventlet.Timeout as te:
            if te != t:
                raise
            [a.kill() for a in acceptors]
示例#8
0
class EventletWorker(AsyncWorker):

    @classmethod
    def setup(cls):
        import eventlet
        if eventlet.version_info < (0,9,7):
            raise RuntimeError("You need eventlet >= 0.9.7")
        eventlet.monkey_patch(os=False)
    
    def init_process(self):
        hubs.use_hub()
        super(EventletWorker, self).init_process()
        
    def timeout_ctx(self):
        return eventlet.Timeout(self.cfg.keepalive, False)

    def acceptor(self, pool):
        try:
            while self.alive:
                try:
                    client, addr = self.socket.accept()
                    pool.spawn_n(self.handle, client, addr)
                except socket.error, e:
                    if e[0] not in (errno.EAGAIN, errno.ECONNABORTED):
                        raise

                if pool.running() > self.worker_connections:
                    continue
                           
                try:
                    hubs.trampoline(self.socket.fileno(), read=True,
                        timeout=self.timeout)
                except eventlet.Timeout:
                    pass
        except eventlet.StopServer:
            pool.waitall()

    def run(self):
        self.socket = GreenSocket(family_or_realsock=self.socket.sock)
        self.socket.setblocking(1)

        pool = eventlet.GreenPool(self.worker_connections)

        acceptor = eventlet.spawn(self.acceptor, pool)

        try:
            while self.alive:
                self.notify()
            
                if self.ppid != os.getppid():
                    self.log.info("Parent changed, shutting down: %s" % self)
                    server.stop()
                    break
                eventlet.sleep(0.1) 
        except KeyboardInterrupt:
            pass

        with eventlet.Timeout(self.timeout, False):
            eventlet.kill(acceptor, eventlet.StopServe)
示例#9
0
文件: worker.py 项目: dahabit/liasis
class Worker(object):

    def __init__(self, sock):
        self.socket = sock

    def init_worker(self):
        hubs.use_hub()

    def run(self):
        self.socket = GreenSocket(family_or_realsock=self.socket.sock)
        self.socket.setblocking(1)
        self.acceptor = eventlet.spawn(eventlet.serve, self.socket, server.handle, int(config.get("MAX_CONNECTIONS")))
        while True:
            eventlet.sleep(0.1)
        with eventlet.Timeout(30, False):
            eventlet.kill(self.acceptor, eventlet.StopServe)
示例#10
0
文件: worker.py 项目: kura/liasis
class Worker(object):
    def __init__(self, sock):
        self.socket = sock

    def init_worker(self):
        hubs.use_hub()

    def run(self):
        self.socket = GreenSocket(family_or_realsock=self.socket.sock)
        self.socket.setblocking(1)
        self.acceptor = eventlet.spawn(eventlet.serve, self.socket,
                                       server.handle,
                                       int(config.get("MAX_CONNECTIONS")))
        while True:
            eventlet.sleep(0.1)
        with eventlet.Timeout(30, False):
            eventlet.kill(self.acceptor, eventlet.StopServe)
示例#11
0
    def run(self):
        acceptors = []
        for sock in self.sockets:
            s = GreenSocket(family_or_realsock=sock)
            s.setblocking(1)
            hfun = partial(self.handle, s)
            acceptor = eventlet.spawn(eventlet.serve, s, hfun,
                    self.worker_connections)

        while self.alive:
            self.notify()
            if self.ppid != os.getppid():
                self.log.info("Parent changed, shutting down: %s", self)
                break

            eventlet.sleep(1.0)

        self.notify()
        with eventlet.Timeout(self.cfg.graceful_timeout, False):
            [eventlet.kill(a, eventlet.StopServe) for a in acceptors]