Python论坛  - 讨论区

标题:[python-chinese] cherryflow 的一些修正

2006年02月05日 星期日 11:01

Albert Lee hanzhupeng at gmail.com
Sun Feb 5 11:01:40 HKT 2006

由于 cherrypy 版本的更新,去掉了 cpg
模块,session也有些变化,因此一些模块无法兼容,cherryflow模块的更新很慢(怀疑有没有人在管),最近在学习 continuation
因此想尝试一下这个 cherryflow
自己动手修改了 cherryflow 中的 common.py

#========== common.py ========
"""
See docstring for flow decorator.
"""

#from cherrypy import cpg, cperror
import cherrypy

import gencopy
import gentree


_counter = 0 # this is the seed variable which assigns each flow a unique
ID; internal

back_button_error = not gencopy.can_copy_generators # should we not allow
the back button or refreshes?

# the following internal functions encompass most of the common
# functionality between the two different decorators (flow and chunked_flow)

def _get_flowid():
    """
    Internal.
    """
    # first, give this flow an id and increment the counter
    global _counter
    flowid = _counter
    _counter += 1
    return flowid

def _check_resume(flowid, _resume):
    """
    Internal.
    """
    global back_button_error
    # if we can't copy generators, we should make sure the client doesn't
abuse the back button and corrupt data
    # because previous resume point state will be corrupted.
    if _resume != cherrypy.session.get("flow_%d_lastresume" % flowid, 0) and
_resume != 0 and back_button_error:
        print "Session Map: %s" % cherrypy.session.get("flow_%d_lastresume"
% flowid, 0)
        print "_resume: %s" % _resume
        print "error: %s" % back_button_error
        # if we are not on the current resumepoint, and we aren't starting
the flow again, and we should display errors...
        raise cherrypy._cperror.InternalError, "Please don't use the back
button or refresh." # display the error
    cherrypy.session["flow_%d_lastresume" % flowid] =
cherrypy.request.resume_id

def _save_resumepoint(flowid, gen):
    """
    Internal.
    """
    cherrypy.session["flow_%d_%d" % (flowid, cherrypy.request.resume_id)] =
gen # save the current resume point...

def _get_gen(flowid, _resume, method, self):
    """
    Internal.
    """
    cherrypy.request.resume_id = _resume + 1 # generate the resume id for
the current resume point
    _check_resume(flowid, _resume)
    # first, try to fetch the saved state from the last resume point
    gen = cherrypy.session.get("flow_%d_%d" % (flowid, _resume), None)
    if not gen or _resume == 0:
        gen = gentree.GeneratorTree(method(self)) # if there was no saved
state, start the flow anew
    return gencopy.copy(gen) # copy the generator so the previous resume
point's state remains untouched


#===== end common.py ============


修改其自带的 example.py 文件如下:
#from cherrypy import cpg
#from cherrypy.lib.filter import generatorfilter
import cherrypy
import cherryflow

class MyController:
    def _miniflow(self, i, j):
        # miniflow exists to demonstrate generator trees
        yield "This is iteration %d. j = %d Next
iteration j++" % (i, j,
cherrypy.request.resume_id, cherrypy.request.resume_id)

    def index(self):
        j = 1
        i = 0
        while True:
            i += 1
            yield self._miniflow(i,j)
            if int(cherrypy.request.paramMap.get("add", 0)) == 1:
                j += 1

    index = cherrypy.expose(cherryflow.flow(index)) # py2.3

    def chunked(self):
        yield "hello "
        yield "world "
        yield "Next" % cherrypy.request.resume_id
        yield cherryflow.SEND_AND_WAIT
        yield "goodbye "
        yield "world"
        yield cherryflow.SEND_AND_WAIT
    chunked = cherrypy.expose(cherryflow.chunked_flow(chunked))

if __name__ == "__main__":
    #cherryflow.back_button_error(True)
    cherrypy.root = MyController()
    cherrypy.config.update({'sessionFilter.on': True})
    cherrypy.server.start()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060205/82c58976/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号