Lua/APR - proper standard library for Lua
peterodding.com
peterodding.com
For my Lua server [1] I currently use the nixio library from the LuCI project and polling instead[2]
Although the downside of both nixio and Lua/APR library is the use of compiled C bindings to manually interact with the virtual machine.
The exciting development with LuaJIT is the high performance FFI that allows you to make system calls and create structs directly from in Lua[3], so I am currently considering switching from nixio to ljsyscall when I do a new version [4]
[1]https://github.com/davidhollander/ox
[2]http://neopallium.github.com/nixio/
Let's say you have 1000 requests for a landing page, and the landing page has a data request that scrapes the entire database to find the popular-maximum-geo-similarity-reccomendation-zone-geo-user-thing list for the entire site, but only updates once every 10 seconds.
Ideally, you want to only send at most 1 request to your database every 10 seconds, without having to open a new connection. If you are doing a polling server and have all 1000 clients waiting for the data, you can trigger a callback to all of them when the database reports back without having to recopy data, use shared memory, do more ipc, or context switches. And for the 1000 clients to pause to wait for that single combined data callback, no extra blocking reads\connections to anything were necessary.
If by static content you are referring to file transfers, polling does not actually work on regular files in Linux. I believe they will always return ready to read even if reading would block. So usually a thread or forked process is actually required when handling files unless you dont mind disk seeks pausing the entire event loop on your server.
First of all: you're just straight-up wrong about file transfers and polling. The select(), epoll(), et. al. syscalls are exactly what you want for large network file transfers. This is not the same as naive "polling", but is what most implementers mean when they talk about an event/polling based server implementations.
Even "green" threading runtimes like MRI and CPython can still multiplex threads in the presence of blocking I/O calls, because the standard C syscalls can easily indicate when the main thread would block while trying to read or write.
Furthermore, a "threaded" server in no way has to allocate a full hardware thread for every client. Single-threaded, callback-based systems may fare nearly as well for I/O-bound workloads, but threads still have real value for many if not most real-world concurrent systems. A pool of worker threads can keep both the CPU and I/O subsystems maximally active without forcing a "shared nothing" architecture onto a system located on a single physical host.
In a world of multicore CPUs, limited L1/L2 cache, and fast kernel schedulers, threaded code can stand toe-to-toe with pure event loops for non-trivial workloads.
I think there's some confusion on your end. My assertion was not that you shouldn't use polling for file transfers, but that you also needs to use at least one additional fork\ thread to do the polling in a second event loop to isolate disk-dependent connections, because polling does not work on the disk part of a file transfer, only the network part.
> standard C syscalls can easily indicate when the main thread would block while trying to read or write
Only on the socket end. Not the regular-file end. This has been the case for a long time on linux with O_NONBLOCK and regular files (aka not pipes\sockets\processes). Which OS\kernel version are you using?
I think the stewards of Lua are afraid to grow the standard library because most of Lua's early popularity stemmed from how small and embeddable it is.
[#] The opinion here is that Lua needs a standard library. Maybe it does, maybe it doesn't, but I want to come to that conclusion myself, that's all.
> APR powers software such as the Apache webserver and Subversion and Lua/APR makes the APR operating system interfaces available to Lua, serving as an extended standard library.
I wrote that description because I like Lua so much that I want to use it not only for embedding but also for writing standalone scripts. However I found that writing standalone Lua scripts that run on both Windows and UNIX is hard because Lua's standard library contains almost no operating system interfaces (e.g. no directory handling and minimal process handling). Also lots of third party libraries either work on Windows or POSIX systems but not both.
After reading your comments and those by compay about Penlight I realized that "standard library" may not be the best way to describe the Lua/APR binding because its focus is really on being an extended operating system interface.
I must admit I also looked through the implementation here and there, when the documentation didn't explain the 'grand scheme' and there were no tests to read :-(
Another one which I prefer personally because it's more comprehensive, is Steve Donovan's Penlight. If you have a background in Python you might enjoy using it:
Sometimes really simple examples can be useful.
There's an older edition available for free online[2].
Then there's of course the lua users wiki and its "Learning Lua" section[3].
I find the wiki a little hard to navigate so I just went ahead and bought the book. IMHO the gems in Chapter 10 (Complete Examples) and the clear explanation of coroutines are alone worth the price. The rest is a very nice bonus too.
[1] http://store.feistyduck.com/products/programming-in-lua [2] http://www.lua.org/pil/ [3] http://lua-users.org/wiki/LearningLua
Like K&R, it's style is colloquially terse, yet each sentence is chock full of information. It's a great book for someone who's already coming from another language like Python.
If you're into graphics programming, then http://love2d.org/ would be the perfect vehicle for you to get into Lua while you read PIL.
https://github.com/justincormack/ljsyscall
this would work with LuaJIT: http://luajit.org/
The FFI in LuaJIT is kind of brilliant. Check yourself.
EDIT: with FFI in LuaJIT you can jump very comfortably between the high-level Lua code and low-level native calls. It's almost addictive. And the code is running X times faster than the Lua baseline (X between single-digit and triple-digit values).
There libraries are: GLFW, SDL, ZeroMQ, AntTweakBar, and planning on few more. There is also OpenCL and OpenGL and glu.
Please visit: http://github.com/malkia/ufo
You should not need to recompile, as I've packaged the binaries.