Lance
--
邮件自: 列表“openresty”,专用于技术讨论!
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
详情: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
建议: 提问的智慧 http://wiki.woodpecker.org.cn/moin/AskForHelp
教程: http://agentzh.org/misc/nginx/agentzh-nginx-tutorials-zhcn.html
2012/9/5 pengqi <feng...@gmail.com>:
> os.execute调用shell的代价很大,而且会阻塞worker,所以建议使用luaposix这一段库来创建目录
>
嗯,是的。
当然,另一种选择是使用 LuaJIT 的 FFI 来从 Lua 直接调用 POSIX 接口,例如我们可以创建下面这个 utils 模块:
-- utils.lua
module("utils", package.seeall)
local ffi = require "ffi"
local C = ffi.C
local errno = ffi.errno
local string = ffi.string
ffi.cdef[[
int mkdir(const char *path, int mode);
char *strerror(int errnum);
]]
function mkdir(path, mode)
if not mode then
mode = 0x1FF
end
local rc = C.mkdir(path, mode)
if rc ~= 0 then
return false, string(C.strerror(errno()))
end
return true
end
然后使用下面这个 nginx location 来测试一下:
location ~ '^/t/(\w+)$' {
content_by_lua '
local utils = require "utils"
local dir = ngx.var[1]
local ok, err = utils.mkdir("/tmp/" .. dir)
if not ok then
ngx.say("failed to mkdir ", dir, ": ", err)
return
end
ngx.say("directory ", dir, " created!")
';
}
请求之:
$ rm -rf /tmp/foo
$ curl localhost:1984/t/foo
directory foo created!
$ curl localhost:1984/t/foo
failed to mkdir foo: File exists
使用 FFI 来调用 C API 在效率上应当会高于传统的 C 模块,因为 JIT 编译器会直接内联 C 调用。
Best regards,
-agentzh
2012/9/5 Lance Li <lance...@gmail.com>:
> io.*也会阻塞worker吗?ffi呢?
>
文件系统相关操作的系统调用都会阻塞,除非启用了 AIO :) 相对于一般更不可控的网络 I/O,少量的磁盘 I/O 的阻塞效应倒不一定会成为瓶颈 :)
Best regards,
-agentzh