[Tutor] help on StringIO

Kent Johnson kent37 at tds.net
Mon May 26 22:45:32 CEST 2008


On Mon, May 26, 2008 at 12:53 PM, inhahe <inhahe at gmail.com> wrote:
> I guess that makes sense, but it seems a lot more useful to have a
> function that only reads what's been written since the last time you
> read.  That way reading from a file that's growing would be
> transparent--the same as reading it as if it were already full size.
> It seems with this read function if you were copying a file, because
> the file might be growing you'd have to do this:
> while 1:
>  p = f1.tell()
>  f2.write(f1.read(4096))
>  f1.seek(p+4096)
> otherwise your copy process would quit as soon as any new data is
> written, because the file position would jump to the end of the file.

You are confusing reading and writing a single file using a single
file handle, and reading from one file and writing to another.
>
> and getvalue just returns the entire contents of the buffer.
>>>> s.write('hi')
>>>> s.getvalue()
> 'hi'
>>>> s.write('hi')
>>>> s.getvalue()
> 'hihi'
>
> so i'd be getting redundant data.
>
> What i'm wondering about specifically is WSGI.  it's supposed to
> return a file-like object, wsgi.input, for reading the http request.
> and if you read beyond what's currently received, it's supposed to
> block until more is received.  so are we actually supposed to read
> from wsgi.input by doing this?
>
> while 1:
>  p = wsgi.input.tell()
>  processe(wsgi.input.read(4096))
>  wsgi.input.seek(p+4096)  # otherwise the next time something is
> added to wsgi.input your reading will return nothing

No, just read from the input.

> and even then i suppose the WSGI server had better not write anything
> to the stream before it's all read up otherwise it'll overwrite to
> some position in the middle of the stream (wherever the current
> position is).  this makes no sense to me.  how is this done? (and
> where's the file class that works in a way that makes sense?)

Can you show an example of why you need this?

Kent

PS Please use Reply All to reply to the list.


More information about the Tutor mailing list