web.py 学习(-)Rocket web框架

Rocket是一个轻量级,多线程,符合WSGI规范的web框架。

Rocket使用一个线程监听连接,接收到连接之后放到Queue中,有worker线程进行处理。

Rocket含有以下属性:

  • method - A string value indicating the type of Worker to use to answer the requests received by Rocket. The default is wsgi and will invoke the WSGIWorker class for handling requests. Go to theMethods section to see all available methods.
  • app_info - A dictionary that holds information that the Worker class specified in method will use for configuration. See the documentation in the Methods section for the Worker class you are using for details on what to put in this dictionary.
  • min_threads - An integer number of minimum Worker threads to run. This number must be greater than 0. Rocket will always have at least min_threads number of threads running at a time unless it is in the process of shutting down.
  • max_threads - An integer number of maximum Worker threads. This number must be greater than min_threads or 0. A max_threads of 0 (zero) indicates there to be no maximum thread count. Rocket will continue generating threads so long as there are unanswered connections in the request queue. If the running environment is limited by how many threads a process can own, consider that in addition to max_threads there will also be a monitor thread and listening thread running.
  • queue_size - An integer number of connections allowed to be queued before Rocket accepts them. This number is passed to the listen() function in the operating system’s socket library. It defaults toNone which either uses the operating system’s maximum or 5 if the OS max is not discoverable.
  • timeout - An integer number of seconds to listen to a connection for a new request before closing it. Defaults to 600.
  • handle_signals - A boolean indicating whether or not Rocket should respond to UNIX-style process signals (if the platform supports signals). Defaults to True.

min_threads 必须是一个大于0的数字,Rocket在同一时刻一定有大于min_threads在执行,默认这个值是10

max_threads 是指最多有多少个线程同时在执行,默认是没有限制的,对于cPython来说因为存在GIL,所以线程数量较多不存在问题;对于Jpython,由于是真正的多线程,如果web的瓶颈在于CPU,建议max_threads为CPU核数的1.5倍,避免多线程切换降低效率。

queue_size 是指默认可以接收的连接个数,通常这个值由系统决定。

Web.py中使用到了Rocket框架,初始代码如下:

class Rocket(object):
    """The Rocket class is responsible for handling threads and accepting and
    dispatching connections."""

    def __init__(self,
                 interfaces=(‘127.0.0.1‘, 8000),
                 method=‘wsgi‘,
                 app_info=None,
                 min_threads=None,
                 max_threads=None,
                 queue_size=None,
                 timeout=600,
                 handle_signals=True):

        self.handle_signals = handle_signals
        self.startstop_lock = Lock()
        self.timeout = timeout

        if not isinstance(interfaces, list):
            self.interfaces = [interfaces]
        else:
            self.interfaces = interfaces

        if min_threads is None:
            min_threads = DEFAULTS[‘MIN_THREADS‘]

        if max_threads is None:
            max_threads = DEFAULTS[‘MAX_THREADS‘]

        if not queue_size:
            if hasattr(socket, ‘SOMAXCONN‘):
                queue_size = socket.SOMAXCONN
            else:
                queue_size = DEFAULTS[‘LISTEN_QUEUE_SIZE‘]

        if max_threads and queue_size > max_threads:
            queue_size = max_threads

        if isinstance(app_info, dict):
            app_info[‘server_software‘] = SERVER_SOFTWARE

        self.monitor_queue = Queue()
        self.active_queue = Queue()

        self._threadpool = ThreadPool(get_method(method),
                                      app_info=app_info,
                                      active_queue=self.active_queue,
                                      monitor_queue=self.monitor_queue,
                                      min_threads=min_threads,
                                      max_threads=max_threads)

        # Build our socket listeners
        self.listeners = [Listener(
            i, queue_size, self.active_queue) for i in self.interfaces]
        for ndx in range(len(self.listeners) - 1, 0, -1):
            if not self.listeners[ndx].ready:
                del self.listeners[ndx]

        if not self.listeners:
            log.critical("No interfaces to listen on...closing.")
            sys.exit(1)

    def _sigterm(self, signum, frame):
        log.info(‘Received SIGTERM‘)
        self.stop()

    def _sighup(self, signum, frame):
        log.info(‘Received SIGHUP‘)
        self.restart()

    def start(self, background=False):
        log.info(‘Starting %s‘ % SERVER_SOFTWARE)

        self.startstop_lock.acquire()

        try:
            # Set up our shutdown signals
            if self.handle_signals:
                try:
                    import signal
                    signal.signal(signal.SIGTERM, self._sigterm)
                    signal.signal(signal.SIGUSR1, self._sighup)
                except:
                    log.debug(‘This platform does not support signals.‘)

            # Start our worker threads
            self._threadpool.start()

            # Start our monitor thread
            self._monitor = Monitor(self.monitor_queue,
                                    self.active_queue,
                                    self.timeout,
                                    self._threadpool)
            self._monitor.setDaemon(True)
            self._monitor.start()

            # I know that EXPR and A or B is bad but I‘m keeping it for Py2.4
            # compatibility.
            str_extract = lambda l: (l.addr, l.port, l.secure and ‘*‘ or ‘‘)

            msg = ‘Listening on sockets: ‘
            msg += ‘, ‘.join(
                [‘%s:%i%s‘ % str_extract(l) for l in self.listeners])
            log.info(msg)

            for l in self.listeners:
                l.start()

        finally:
            self.startstop_lock.release()

        if background:
            return

        while self._monitor.isAlive():
            try:
                time.sleep(THREAD_STOP_CHECK_INTERVAL)
            except KeyboardInterrupt:
                # Capture a keyboard interrupt when running from a console
                break
            except:
                if self._monitor.isAlive():
                    log.error(traceback.format_exc())
                    continue

        return self.stop()

    def stop(self, stoplogging=False):
        log.info(‘Stopping %s‘ % SERVER_SOFTWARE)

        self.startstop_lock.acquire()

        try:
            # Stop listeners
            for l in self.listeners:
                l.ready = False

            # Encourage a context switch
            time.sleep(0.01)

            for l in self.listeners:
                if l.isAlive():
                    l.join()

            # Stop Monitor
            self._monitor.stop()
            if self._monitor.isAlive():
                self._monitor.join()

            # Stop Worker threads
            self._threadpool.stop()

            if stoplogging:
                logging.shutdown()
                msg = "Calling logging.shutdown() is now the responsibility of                        the application developer.  Please update your                        applications to no longer call rocket.stop(True)"
                try:
                    import warnings
                    raise warnings.DeprecationWarning(msg)
                except ImportError:
                    raise RuntimeError(msg)

        finally:
            self.startstop_lock.release()

    def restart(self):
        self.stop()
        self.start()

start方法中执行下列步骤:

1、启动关闭锁加锁

2、注册终止和重启的信号

3、启动worker线程池

4、启动监控线程,添加监控线程到线程池中

5、启动所有的监听线程

6、释放启动关闭锁

7、进入while(监控线程存活)循环,每次休眠1s

stop方法:

1、启动关闭锁加锁

2、所有监听线程设置ready标记为为False

3、等待所有监听线程结束

4、等待监控线程结束

5、关闭worker线程池

6、输出关闭log

7、释放启动关闭锁

时间: 2024-10-24 23:15:01

web.py 学习(-)Rocket web框架的相关文章

精简网络框架web.py学习笔记 -《狗嗨默示录》-

web.py 内置了web服务器,代码写完后,将其保存,例如文件名为mywebpy.py,可以用下面的方法来启动服务器: python mywebpy.py 打开你的浏览器输入 http://localhost:8080/ 查看页面. 若要制定另外的端口,使用 python mywebpy.py 1234. URL 处理 任何网站最重要的部分就是它的URL结构.你的URL并不仅仅只是访问者所能看到并且能发给朋友的.它还规定了你网站运行的心智模型.在一些类似del.icio.us的流行网站 , U

web.py学习心得

1.注意判断数字时,如果是get传递的参数,一定要用int转换.不然出错. 2.$var 定义时,冒号后的内容不是python内容,需加上$符号.如$var naviId:$naviId. 3.各个模板中的变量,要对应一致.在用base布局时,整个模板内容为layout模板的content,模板内定义的变量x(模板变量),在layout模板内用content.x 引用. ----------------------------------  网络上搜索的其他faq  --------------

ASP.NET MVC Web API 学习笔记---Web API概述及程序示例

1. Web API简单说明 近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集成功能,以及通过在浏览器中使用 JavaScript来创建更丰富的HTML体验.所以我相信Web API会越来越有它的用武之地. 说道Web API很多人都会想到Web服务,但是他们仍然有一定的区别:Web API服务是通过一般的 HTTP公开了,而不是通过更正式的服务合同 (如SOAP) 2. ASP.NET W

web.py 学习(二)Worker

Rocket Server 启动一个线程监听客户端的连接,收到连接将连接放置到队列中.线程池中的Worker会以这个连接进行初始化.Rocket中Worker的基类是: class Worker(Thread): """The Worker class is a base class responsible for receiving connections and (a subclass) will run an application to process the the

Python简单Web框架web.py实例hello world

1.安装web.py模块easy_install web.py 2.实现代码 import web urls = ('/hello', 'hello', ) class hello(object): def GET(self): return 'hello world' if __name__ == "__main__": app = web.application(urls, globals()) app.run()

10个用于Web开发的最好 Python 框架

Python 是一门动态.面向对象语言.其最初就是作为一门面向对象语言设计的,并且在后期又加入了一些更高级的特性.除了语言本身的设计目的之外,Python标准 库也是值得大家称赞的,Python甚至还自带服务器.其它方面,Python拥有足够多的免费数据函数库.免费的Web网页模板系统.还有与Web服务 器进行交互的库.这些都可以设计到你的Web应用程序里面.在这篇文章里,我们将为Python Web开发者介绍基于Python的10大Web应用框架. CubicWeb CubicWeb的最重要的

windows下apache+wsgi+web.py环境搭建

首先安装好wsgi模块并启用:1.下载地址:我本机是python2.7 http://code.google.com/p/modwsgi/downloads/detail?name=mod_wsgi-win32-ap22py27-3.3.so2.把mod_wsgi-win32-ap22py27-3.3.so放到apache安装目录下的modules目录下3.打开 http.conf添加:LoadModule wsgi_module modules/mod_wsgi-win32-ap22py27-

CentOS7+Python3.6利用web.py库进行微信公众平台服务器简易配置,token验证

1.安装配置Python CentOS7 自带 Python2.7, 我用的是Python3.6.5,需要CentOS7安装配置Python3并和Python2共存的,请看以下教程: CentOS7安装配置Python3.6.5 2.安装web.py pip install web.py 可以换国内源下载,速度比较快 3.安装libxml2, libxslt, lxml python yum install libxml2 yum install libxslt yum install lxml

Web.py 框架学习笔记 - URL处理

最近由于工作需要开始学习基于python的web应用框架web.py.为了方便学习,将学习心得逐日记下以便日后复习. URL 模板: web.py提供了一套url处理的模板,在python工程中,只需要将网页结构配置为一个元组(tuple)即可.该元组在web.py中的解析模式为:url路径,对应处理url的类名.这里,类名必须和service代码中对应的类名一致.例如, 1 urls = ( 2 "/tasks/?", "signin", 3 "/tas