kernelsauce / turbo

Turbo is a framework built for LuaJIT 2 to simplify the task of building fast and scalable network applications. It uses a event-driven, non-blocking, no thread design to deliver excellent performance and minimal footprint to high-load applications while also providing excellent support for embedded uses.
http://turbo.readthedocs.io/
Apache License 2.0
528 stars 84 forks source link

Question about async file write #150

Closed Mynock17 closed 9 years ago

Mynock17 commented 9 years ago

Hi,

what is the right (turbo-) way to write non blocking to a file (i.e. for logging)?

Regards Bernd

kernelsauce commented 9 years ago

Hi. Well its a complicated issue with many solutions...

Writing to a disk as you may know is blocking on Linux at least. Epoll or any others option does not work for disk files. In a C program the usual way would be to copy data to a buffer which another thread would write to disk, so the main thread could continue running. Unfortunately we do not have real OS threads in Lua.

The quick solution for logging at least would be to use piping from stdout and strerr to a file, another way would be to log using turboredis sub/pub? You can reimplement the turbo.log.functions any way you like in your application and the application itself will run using your functions. You could write a C extension which runs a thread that writes to logs or something. Personally I would probably just write to log files straight from Turbo or pipe data to files. If you are writing big files, you can write in chunks (say in 1MB size) and simply do a coroutine.yield() for every chunk, to make sure that other coroutines gets some execution time...

For distributed systems that runs over multiple nodes I would use Redis or some others system to gather logs from all nodes.

naturallymitchell commented 8 years ago

The quick solution for logging at least would be to use piping from stdout and strerr to a file

http://stackoverflow.com/questions/6674327/redirect-all-output-to-file

Thanks, @kernelsauce