How to effectively write a large file asynchronously in append mode?

268 views
Skip to first unread message

rajde...@gmail.com

unread,
Sep 17, 2020, 5:23:58 AM9/17/20
to Tornado Web Server
Hello , 

i am trying to see if a large file can be written to in append mode from tornado from a stream of incoming data without actually hampering tornado's core performance with other things. 

I have looked at aiofile but i cant find the proper examples of writing a file continuously. Can anyone provide some inputs?

Regards

agn...@gmail.com

unread,
Sep 19, 2020, 1:21:46 AM9/19/20
to Tornado Web Server
look at this issues #231

Rajdeep Rath

unread,
Sep 19, 2020, 1:37:20 AM9/19/20
to python-...@googlegroups.com
Thanks for answering me 👏. However am little confused. Are you sure that is what will help me? Cause I am not looking to read data from post typically. My data could very well be In a file already or coming from a socket. I basically need to know how I can keep writing the data to large file in append mode. If the file already has data I would want to add to it and so on.

--


You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.


To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.


To view this discussion on the web visit https://groups.google.com/d/msgid/python-tornado/b32775e3-0b04-4e24-8206-6c23562c2ffcn%40googlegroups.com.


Shane Spencer

unread,
Sep 19, 2020, 1:50:14 AM9/19/20
to python-...@googlegroups.com
aiofile is a good python module that addresses writing async

--

Michael DePalatis

unread,
Sep 19, 2020, 2:56:50 PM9/19/20
to Tornado Web Server
`stream_request_body` is precisely what you want. It is documented here: https://www.tornadoweb.org/en/stable/web.html#tornado.web.stream_request_body (there is a link to a file upload demo there). Here's the basic idea of usage:

from tornado.ioloop import IOLoop
from tornado.web import Application, RequestHandler, stream_request_body

@stream_request_body
class MyHandler(RequestHandler):
    def prepare(self):
        self.file_obj = open("output.txt", "ab")

    def on_finish(self):
        self.file_obj.close()

    def data_received(self, data):
        self.file_obj.write(data)

    def post(self):
        print("data received")
        self.write("ok")

if __name__ == "__main__":
    handlers = [("/", MyHandler)]
    app = Application(handlers)
    app.listen(8999)
    IOLoop.current().start()

You just need to implement the `data_received` which operates on each chunk of data as it comes in. Note that in this case there's no async/await, but `prepare`, `data_received`, and `post` may all be async if you want.

Rajdeep Rath

unread,
Sep 19, 2020, 9:39:34 PM9/19/20
to python-...@googlegroups.com
But I don’t see an example of writing large file in append mode anywhere.

Rajdeep Rath

unread,
Sep 19, 2020, 9:41:18 PM9/19/20
to python-...@googlegroups.com
Michael

Thanks. But what if I am not even using http post or get ? How will stream body help in writing data from somewhere else to a file in file system async.?

You received this message because you are subscribed to a topic in the Google Groups "Tornado Web Server" group.


To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-tornado/_at18vcqx_g/unsubscribe.


To unsubscribe from this group and all its topics, send an email to python-tornad...@googlegroups.com.


To view this discussion on the web visit https://groups.google.com/d/msgid/python-tornado/df44fa27-99cf-4bbc-b442-623336de0b02n%40googlegroups.com.


Shane Spencer

unread,
Sep 19, 2020, 11:22:18 PM9/19/20
to python-...@googlegroups.com
There are async file libraries on pypi.

Tornado uses python asyncio loop now by default.  You can use async and await along with libs like aiofile to do async file stuffs.

Beyond that not sure how to better answer your question

Rajdeep Rath

unread,
Sep 19, 2020, 11:46:59 PM9/19/20
to python-...@googlegroups.com
Ok thanks. I did check that out. I guess I have to ask the developer for a append mode example. Looks like that is what I need. Currently I can only see write and read examples.

Regards

Shane Spencer

unread,
Sep 20, 2020, 12:39:43 AM9/20/20
to python-...@googlegroups.com

Shane Spencer

unread,
Sep 20, 2020, 12:40:55 AM9/20/20
to python-...@googlegroups.com
Sorry.  I mean ‘a’.  

Rajdeep Rath

unread,
Sep 20, 2020, 1:34:21 AM9/20/20
to python-...@googlegroups.com
Ok thank you so much. I will check that out. 👍

Reply all
Reply to author
Forward
0 new messages