As already noted, there are several issues with the script that prevent you from getting what you want:
os.execute("...")
is blocked until the command is completed and in your case it doesn't complete (as it runs an infinite loop). Solution: you need to detach that process from yours by using something likeio.popen()
instead ofos.execute()
co = coroutine.create( startServer() )
doesn't create a coroutine in your case.coroutine.create
call accepts a function reference and you pass it the result ofstartServer
call, which isnil
. Solution: useco = coroutine.create( startServer )
(note that parenthesis are dropped, so it's not a function call anymore).- You are not yielding from your coroutines; if you want several coroutines to work together, they need to be cooperating by giving control to each other when appropriate. That's what
yield
command is for and that's why it's called non-preemptive multithreading. Solution: you need to use a combination ofresume
andyield
calls after youcreate
your coroutine. startServer
doesn't need to be a coroutine as you are not giving control back to it; its only purpose is to start the server.
In your case, the solution may not even need coroutines as all you need to do is: (1) start the server and let it detach from your process (for example, using popen
) and (2) work with your process using whatever communication protocol it requires (pipes, sockets, etc.).
There are more complex and complete solutions (like LuaLanes) and also several good descriptions on creating simple coroutine dispatchers.