Beispiel #1
0
    def start(self):
        log.debug(
            'Starting rest api',
            host=self.config['host'],
            port=self.config['port'],
        )

        # WSGI expects an stdlib logger. With structlog there's conflict of
        # method names. Rest unhandled exception will be re-raised here:
        wsgi_log = logging.getLogger(__name__ + '.pywsgi')

        # server.stop() clears the handle and the pool, this is okay since a
        # new WSGIServer is created on each start
        pool = gevent.pool.Pool()
        wsgiserver = WSGIServer(
            (self.config['host'], self.config['port']),
            self.flask_app,
            log=wsgi_log,
            error_log=wsgi_log,
            spawn=pool,
        )

        try:
            wsgiserver.init_socket()
        except socket.error as e:
            if e.errno == errno.EADDRINUSE:
                raise APIServerPortInUseError()
            raise

        self.wsgiserver = wsgiserver

        log.debug('REST API started',
                  node=pex(self.rest_api.raiden_api.address))

        super().start()
Beispiel #2
0
    def make_server(self, app, port, hostname, handler_class, direct=False):
        server = WSGIServer((hostname, port), app, handler_class=handler_class)
        server.init_socket()
        self.port = server.address[1]

        self.server = server
        if direct:
            self.ge = None
            self._run(server, self.port)
        else:
            self.ge = spawn(self._run, server, self.port)
Beispiel #3
0
    def make_server(self, app, port, hostname, handler_class, direct=False):
        server = WSGIServer((hostname, port), app, handler_class=handler_class)
        server.init_socket()
        self.port = server.address[1]

        self.server = server
        if direct:
            self.ge = None
            self._run(server, self.port)
        else:
            self.ge = spawn(self._run, server, self.port)
Beispiel #4
0
    def start(self, host='127.0.0.1', port=5001):
        # WSGI expects an stdlib logger. With structlog there's conflict of
        # method names. Rest unhandled exception will be re-raised here:
        wsgi_log = logging.getLogger(__name__ + '.pywsgi')
        wsgiserver = WSGIServer(
            (host, port),
            self.flask_app,
            log=wsgi_log,
            error_log=wsgi_log,
        )

        try:
            wsgiserver.init_socket()
        except socket.error as e:
            if e.errno == errno.EADDRINUSE:
                raise APIServerPortInUseError()
            raise

        self.wsgiserver = wsgiserver
        super().start()
Beispiel #5
0
    def make_server(self, app, port, hostname, handler_class, direct=False):
        """Creates and starts the server. If direct is true the server is run
        in the current thread otherwise in a greenlet.

        :param app: The WSGI application instance to be used
        :param int port: The port the server is to listen on
        :param str hostname: The hostname the server is to use
        :param handler_class: The class to be used for handling WSGI requests
        :param bool direct: T/F indicating if the server should be run in a greenlet
        or in current thread
        """
        server = WSGIServer((hostname, port), app, handler_class=handler_class)
        server.init_socket()
        self.port = server.address[1]

        self.server = server
        if direct:
            self.ge = None
            self._run(server, self.port)
        else:
            self.ge = spawn(self._run, server, self.port)
Beispiel #6
0
class HTTPServer(object):
    """
    A simple http wrapper
    """
    def __init__(self, host, port, processes, logfile, verbose, work_dir):
        self.host = host
        self.port = port
        self.processes = processes
        self.logfile = logfile
        self.verbose = verbose
        self.work_dir = work_dir
        setup_logging(self.logfile, self.verbose, self.work_dir)
        self.server = WSGIServer((self.host, self.port), app, backlog=100000, log=logger)
        self.server.init_socket()

    def serve_forever(self):
        self.server.start_accepting()
        self.server._stop_event.wait()
        gevent.wait()

    def start(self):
        for i in range(self.processes):
            p = Process(target=self.serve_forever, args=tuple())
            p.start()
            PROCESSES.append(p)
        try:
            print "Start listener"
            for p in PROCESSES:
                p.join()
        except KeyboardInterrupt:
            for p in PROCESSES:
                print "stopping %s" % p.name
                p.terminate()
        except:
            pass
        finally:
            print "Done"
            sys.exit(0)
from gevent import fork
from prefork import prefork, cpu_count


def connection(env, start):
    ws = env["wsgi.websocket"]
    send, rcv = ws.send, ws.receive
    while True:
        send(rcv())


if __name__ == "__main__":
    addr = "0.0.0.0", 8000
    server = WSGIServer(addr, connection, handler_class=WebSocketHandler, log=None)

    # bind socket
    if hasattr(server, "pre_start"):
        server.pre_start()
    else:
        server.init_socket()

    num = cpu_count()
    print "Gevent + gevent-websocket (%s workers)" % (num + 1)
    prefork(num, fork=fork)  # gevent needs special fork

    try:
        server.serve_forever()
    except KeyboardInterrupt:
        server.stop()
from gevent.pywsgi           import WSGIServer
from geventwebsocket.handler import WebSocketHandler

from gevent                  import fork
from prefork                 import prefork, cpu_count

def connection( env, start ):
    ws = env["wsgi.websocket"]
    send, rcv = ws.send, ws.receive
    try:
        while True: send( rcv() )
    except:
        pass

if __name__ == "__main__":
    addr   = "0.0.0.0", 8000
    server = WSGIServer(addr, connection, handler_class=WebSocketHandler, backlog=768, log=None)

    if hasattr( server, 'pre_start' ):
        server.pre_start()
    else:
        server.init_socket()

    num = cpu_count()
    print "Gevent + gevent-websocket (%s workers)" % num
    if prefork( num, fork=fork ): exit()  # gevent needs a special fork

    server.serve_forever()
Beispiel #9
0
 def init_socket(self):
     WSGIServer.init_socket(self)