gpt4 book ai didi

Lua 对空 FIFO 的非阻塞读访问

转载 作者:行者123 更新时间:2023-12-04 06:10:40 32 4
gpt4 key购买 nike

我正在编写一个从 FIFO 读取数据的小 lua 脚本。为此,我使用经典的:

f=assert(io.open("/tmp/myfifo")
f:read()

当 fifo 为空/未馈送时,我的脚本块。有没有办法避免这种情况?

最佳答案

我猜没有直接的 Lua-only 方法。与 luajit http://luajit.org/ (提供 ffi)有可能:

 local ffi = require'ffi'

--- The libc functions used by this process.
ffi.cdef[[
int open(const char* pathname, int flags);
int close(int fd);
int read(int fd, void* buf, size_t count);
]]
local O_NONBLOCK = 2048
local chunk_size = 4096
local buffer = ffi.new('uint8_t[?]',chunk_size)
local fd = ffi.C.open('mypipe',O_NONBLOCK)
local nbytes = ffi.C.read(fd,buffer,chunksize)
-- .. process data

关于Lua 对空 FIFO 的非阻塞读访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7820356/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com