You cannot create true simultaneous processing only with coroutines - coroutines are designed for joint multitasking. Only one coroutine is executed at a time.
The code you wrote is no different from calling GiveMessage() in a loop directly. You need to write a coroutine manager and find a reasonable reason for exiting GiveMessage() for this approach to work.
There are at least three solutions, depending on the specifics of your task:
Create several forks of your server, process operations in coroutines in each fork. Manage coroutines with either Copas or lua-ev or with a homegrown dispatcher, thereβs nothing wrong with that. I recommend this way.
Use Lua states instead of coroutines, keep a state pool, a thread pool of a working operating system, and a task queue. Complete each task in a free Lua state with a free workflow. Some low level coding is required and randomly.
Look for existing more specialized solutions - there are several, but to advise that I need to know better which server you are writing.
Whatever you choose, avoid using the same Lua state from multiple threads at the same time. (Maybe with the right amount of coding, but a bad idea.)
source share