networking - How can a LuaSocket server handle several requests simultaneously? -
the problem inability of lua server accept multiple request simultaneously. attempted make each client message processed in on coroutine, seems have failed.
while true local client = server:accept() coroutine.resume(coroutine.create( function() givemessage( client ) end ) ) end
this code seems not accept more 1 client message @ same time. wrong method? thank helping.
you not able create true simultaneous handling coroutines — coroutines cooperative multitasking. 1 coroutine executed @ same time.
the code you've wrote no different calling givemessage()
in loop directly. need write coroutine dispatcher , find sensible reason yield givemessage()
approach work.
there least 3 solutions, depending on specifics of task:
spawn several forks of server, handle operations in coroutines in each fork. control coroutines either copas or lua-ev or home-grown dispatcher, nothing wrong that. recommend way.
use lua states instead of coroutines, keep pool of states, pool of worker os threads , queue of tasks. execute each task in free lua state free worker thread. requires low-level coding , messier.
look existing more specialized solutions — there several, advice on need know better kind of server you're writing.
whatever choose, avoid using single lua state several threads @ same time. (it possible, right amount of coding, bad idea.)
Comments
Post a Comment