Hello!
On Tue, Aug 5, 2014 at 3:24 AM, ashish adhav wrote:
> In my previous question I had enquired about using languauges other than lua
> [ c/c++] for open resty.
> Accordingly I am using the ffi approach , writing .so and loading them via
> lua.
> But the problem is I am not able to get async behavior , that can be
> obtained by using lua or inbuilt modules .
[...]
> handles the sleep asynchornously .
> I want to achieve this in C for curl , file io , etc .
Use the Lua API provided by ngx_lua to do I/O. For example, use the
cosocket API to do network I/O:
https://github.com/openresty/lua-nginx-module#ngxsockettcp
https://github.com/openresty/lua-nginx-module#ngxsocketudp
Only use C/FFI for CPU-bound calculations (and that's where native
code can really shine).
Regarding file I/O, there is no nonblocking file I/O at least in
Linux. Usually nginx itself just uses blocking I/O itself in many
places as long as the blocking period is expected to be small enough
(and yeah, do not use slow disks). Nginx also supports native AIO but
it's quite limited (and broken) on Linux at least but it can still be
a fun project to expose the AIO API to the Lua land in ngx_lua
(patches welcome :)). Another common work-around is to introduce extra
OS threads to handle blocking file I/O operations and it can also be
an interesting feature to explore in ngx_lua though OS threads have
their own nontrivial overhead and complexity. Yet another common
solution is to put massive file I/O operations into a dedicated daemon
server and let nginx talk to it over sockets instead.
Regards,
-agentzh