我试图在Luvit中使用uv thread worker进行通常会阻止的某些外部调用。因此,我尝试使用命名管道对线程工作者的结果进行序列化……而这是行不通的: 似乎没有调用客户端的连接回调,尽管从打印的消息中,将调用服务器的监听回调。
为简便起见,我将展示一个类似的基于定时发射器的示例来模拟事件:
local uv=require("uv")
local Emitter=require("core").Emitter
local thread=require("thread")
local timer = require("timer")
local beacon=Emitter:new()
timer.setInterval(500,function()
beacon:emit("emit")
end)
local work = thread.work(function(mess)
local uv = require("uv")
local pipewrite=function(mess)
local SOCK = "\\\\.\\pipe\\echo.sock"
local client = uv.new_pipe(false)
local connect_cb=function(err)
assert(not err, err)
thisisnotthecommandyourelookingfor()
p("connecting")
client:write(mess)
end
client:connect(SOCK,connect_cb)
end
pipewrite(mess)
end,function() end)
beacon:on('emit',function()
p("emitting")
thread.queue(work,"emmited")
end)
local SOCK = "\\\\.\\pipe\\echo.sock"
local server = uv.new_pipe(false)
local ret, err, code = server:bind(SOCK)
-- if file already exists, remove it first and try again
if not ret and code == "EADDRINUSE" then
local fs = require("fs")
fs.unlinkSync(SOCK)
_, err, _ = server:bind(SOCK)
assert(not err, err)
else
assert(not err, err)
end
server:listen(128, function (err)
assert(not err, err)
local client = uv.new_pipe(false)
server:accept(client)
p("connected")
client:read_start(function (err, chunk)
assert(not err, err)
if chunk then
print("Got: " .. chunk)
-- client:write(chunk)
else
client:shutdown()
client:close()
end
end)
end)
uv.run()
有人为什么不打印正在连接消息并且为什么这不是您正在寻找的命令错误()? :)
谢谢