cStringIO, leyendo bytes en fragmentos hasta EOF?

I want to read a string in chunks of 1MB at a time, then upload it over XMLRPC, would this be the best way to do this? Is there anything I should watch out for?

while data.read(1048576) != None:
    data.seek(1048576, 1)
    if not rpc_srv.chunk_upload(tf_uuid, data_chunk):
        raise Exception('Failed to upload data.')

preguntado el 24 de agosto de 12 a las 10:08

1 Respuestas

Puede utilizar el iter() función with a sentinel to simplify your loop:

for data_chunk in iter(lambda: data.read(1048576), ''):
    if not rpc_srv.chunk_upload(tf_uuid, data_chunk):
        raise Exception('Failed to upload data.')

No hay necesidad de .seek(), la .read() call already updates the position.

Respondido 24 ago 12, 10:08

after implementing this, i've started getting "Memory Error"'s - jharwood

@Jharwood: create a new question with a full traceback. It could be a totally unrelated problem somewhere else, or it could be the size of your cStringIO instances, for example. - Martijn Pieters

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.