Skip to content

Instantly share code, notes, and snippets.

@juntatalor
Last active October 24, 2018 13:42
Show Gist options
  • Save juntatalor/409dcfcd455cd4903b7ea56be6d8ea38 to your computer and use it in GitHub Desktop.
Save juntatalor/409dcfcd455cd4903b7ea56be6d8ea38 to your computer and use it in GitHub Desktop.
Async file write with tornado
import os
from uuid import uuid4
from tornado import web
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.iostream import PipeIOStream
IOLoop.configure('tornado.platform.asyncio.AsyncIOMainLoop')
class SafeFileIOStream:
def __init__(self, fname):
self.fname = fname
def __enter__(self):
# Create file
os.open(self.fname, os.O_CREAT)
# Create stream
fd = os.open(self.fname, os.O_WRONLY)
self.stream = PipeIOStream(fd)
return self.stream
def __exit__(self, exc_type, exc_val, exc_tb):
# Close stream
self.stream.close()
class TestHandler(web.RequestHandler):
async def get(self):
fname = '/tmp/' + uuid4().hex
with SafeFileIOStream(fname) as stream:
await stream.write(b'hello stranger')
self.write('success')
app_handlers = [
(r'/test/?', TestHandler),
]
application = web.Application(app_handlers)
# application.listen(8000, '0.0.0.0')
if __name__ == '__main__':
server = HTTPServer(application)
server.bind(8000)
server.start(0)
loop = IOLoop.current()
loop.start()
@sebrestin
Copy link

This may seem async, because the server is using multiple processes, thus it executes requests in parallel. If you limit the processes to one (server.start(1)), the server will be sync and not asyc. Also, try using bigger files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment