简体   繁体   English

将数据传递给mod_wsgi

[英]Passing data to mod_wsgi

In mod_wsgi I send the headers by running the function start_response(), but all the page content is passed by yield/return. 在mod_wsgi中,我通过运行功能start_response()发送标题,但所有页面内容均通过yield / return传递。 Is there a way to pass the page content in a similar fashion as start_response()? 有没有一种方法可以像start_response()一样传递页面内容? Using the return.yield statement is very restrictive when it comes to working with chunked data. 在处理分块数据时,使用return.yield语句的限制非常严格。

Eg 例如

def Application():

    b = buffer()

    [... page code ...]

    while True:
        out = b.flush()    
        if out:
            yield out

class buffer:

    def __init__(self):        
        b = ['']
        l = 0

    def add(self, s):
        s = str(s)
        l += len(s)
        b.append(s)

    def flush(self):

        if self.l > 1000:
            out = ''.join(b)
            self.__init__()
            return out

I want to have the buffer outputting the content as the page loads, but only outputs the content once enough of it has piled up (in this eg. 1000 bytes). 我想让缓冲区在页面加载时输出内容,但仅在内容堆积了足够的时间(例如1000字节)后才输出内容。

No; 没有; But I don't think it is restrictive. 但是我不认为这是限制性的。 Maybe you want to paste an example code where you describe your restriction and we can help. 也许您想在示例代码中粘贴您的限制说明,我们可以为您提供帮助。

To work with chunk data you just yield the chunks: 为了与块数据工作,你只yield了大块:

def application(environ, start_response):
    start_response('200 OK', [('Content-type', 'text/plain')]
    yield 'Chunk 1\n'    
    yield 'Chunk 2\n'    
    yield 'Chunk 3\n'
    for chunk in chunk_data_generator():
        yield chunk

def chunk_data_generator()
    yield 'Chunk 4\n'
    yield 'Chunk 5\n'

EDIT : Based in the comments you gave, an example of piling data up to a certain length before sending forward: 编辑 :根据您给的评论,在转发之前将数据堆积到一定长度的示例:

BUFFER_SIZE = 10 # 10 bytes for testing. Use something bigger
def application(environ, start_response):
    start_response('200 OK', [('Content-type', 'text/plain')]
    buffer = []
    size = 0
    for chunk in chunk_generator():
        buffer.append(chunk)
        size += len(chunk)
        if size > BUFFER_SIZE:
            for buf in buffer:
                yield buf
            buffer = []
            size = 0

def chunk_data_generator()
    yield 'Chunk 1\n'    
    yield 'Chunk 2\n'    
    yield 'Chunk 3\n'
    yield 'Chunk 4\n'
    yield 'Chunk 5\n'

如果您不想更改WSGI应用程序本身以在发送响应数据之前部分缓冲响应数据,请实现一个WSGI中间件,该中间件包装WSGI应用程序并执行该任务。

It is possible for your application to "push" data to the WSGI server: 您的应用程序可能会将数据“推送”到WSGI服务器:

Some existing application framework APIs support unbuffered output in a different manner than WSGI. 某些现有的应用程序框架API以与WSGI不同的方式支持无缓冲输出。 Specifically, they provide a "write" function or method of some kind to write an unbuffered block of data, or else they provide a buffered "write" function and a "flush" mechanism to flush the buffer. 具体来说,它们提供某种“写”功能或某种方法来写入未缓冲的数据块,否则它们提供一种缓冲的“写”功能和一种“刷新”机制来刷新该缓冲区。

Unfortunately, such APIs cannot be implemented in terms of WSGI's "iterable" application return value, unless threads or other special mechanisms are used. 不幸的是,除非使用线程或其他特殊机制,否则无法根据WSGI的“可迭代”应用程序返回值来实现此类API。

Therefore, to allow these frameworks to continue using an imperative API, WSGI includes a special write() callable, returned by the start_response callable. 因此,为了允许这些框架继续使用命令式API,WSGI包括一个特殊的write()调用,由start_response调用返回。

New WSGI applications and frameworks should not use the write() callable if it is possible to avoid doing so. 如果可能的话,新的WSGI应用程序和框架不应使用write()可调用。

http://www.python.org/dev/peps/pep-0333/#the-write-callable http://www.python.org/dev/peps/pep-0333/#the-write-callable

But it isn't recommended. 但是不建议这样做。

Generally speaking, applications will achieve the best throughput by buffering their (modestly-sized) output and sending it all at once. 一般而言,应用程序将通过缓冲(中等大小的)输出并立即发送所有输出来实现最佳吞吐量。 This is a common approach in existing frameworks such as Zope: the output is buffered in a StringIO or similar object, then transmitted all at once, along with the response headers. 这是Zope等现有框架中的常用方法:将输出缓冲在StringIO或类似的对象中,然后与响应头一起一次全部传输。

The corresponding approach in WSGI is for the application to simply return a single-element iterable (such as a list) containing the response body as a single string. WSGI中的相应方法是使应用程序简单地返回一个包含响应主体作为单个字符串的单元素可迭代对象(例如列表)。 This is the recommended approach for the vast majority of application functions, that render HTML pages whose text easily fits in memory. 对于绝大多数应用程序功能,建议使用此方法,该功能可呈现文本易于容纳在内存中的HTML页面。

http://www.python.org/dev/peps/pep-0333/#buffering-and-streaming http://www.python.org/dev/peps/pep-0333/#buffering-and-streaming

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM