LuaLanes和LuaSockets
我正在研究一个小的Lua应用程序(在Lua for
Windows下,如果这很重要),它使用套接字与外界进行通信. (LuaSocket)
而我正试图并行提出几个请求.所以我认为LuaLanes是要走的路. (当然,如果有更好的解决方案,我会接受替代方案,但我不愿意为此处理协程.) 像这样的东西: server = assert (socket.bind ('*',1234)) client = server : accept () -- set id to some unique value allClients [id] = client theLane = lanes.gen ("",laneTest) ( id ) print (theLane [1]) 其中laneTest函数定义如下: function laneTest (id) local client = allClients [id] print ('peer: ',client:getpeername()) end 我的问题是在laneTest函数内部,当作为一个通道运行时,我收到这个可爱的错误信息:
(来自客户端:getpeername()) 所以..我不确定这里发生了什么?车道与插座不兼容,还是我做错了什么? 我猜可能是随Lua for Windows一起提供的版本的版本很古老(luaforwindows)并且不适用于套接字,但最新版本可能? (车道2.0.4对比最近的3.xx) 我真的不知道怎么去更新我所拥有的Lanes版本,否则我现在就试过了,所以.我很感激任何建议,如果那是我可以前进的地方,或者有更明显的事情,我做错了. 编辑: 编辑2: require ('socket') require ('lanes') local allClients = {} function theLane (id) print ('the id:',id) -- correctly prints out the id passed to the function local SOCKET = require ('socket') local client = allClients [id] print ('peer:',client:getpeername()) client : close () end local server = assert (SOCKET.bind ('*',1234)) local ip,port = server:getsockname () local laneFunc = lanes.gen('',theLane) local client = server:accept () allClients [1] = client local x = laneFunc (1) print (x[1]) >这无法声明:尝试调用全局’require'(零值) 我提前道歉,因为错过了显而易见的事,但是……你如何让插座与车道一起工作? 编辑3: 好吧,我正在编辑这个以供将来参考:) 据我所知,没有修补luasockets就无法使用Lanes with Sockets.有关详细信息,请参见讨论here;但简而言之(正如Deco的答案中所解释的那样):通道不适用于userdata. luasocket没有提供任何其他方式来访问套接字/套接字信息. 我不想修补luasocket,因为我宁愿使用车道,我要继续前进并坚持使用copas或couroutines. 谢谢大家! 解决方法
编程Lua在
non-preemptive multithreading(使用协同程序)上有一个例子,你几乎可以直接使用它.在我看来,协同程序将是一个更好的解决方案用于您的用例.
还有copas library将自己描述为“基于可由TCP / IP服务器使用的协同程序的调度程序”,但您实际上也可以使用它来异步发送请求(使用addthread和step调用的组合). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |