Skynet of the process

Source: Internet
Author: User
Tags message queue

Before the impression of Skynet, mainly from my understanding of Golang, the experience of gevent development, as well as the Cloud Wind blog. For the underlying code, it is not carefully read. Recently in the implementation of the business system, found that colleagues in the same function to do a mutually exclusive judgment, only to find that the understanding of Skynet wrong.

Previously, when implementing a game server with the Python gevent framework, 3-5 Greenlet were created for each player to handle timer events on players and IO operations. Then there are a handful of process handlers that handle global timer events. Of course, the battle is implemented in a separate process, where there are basically 1-2 Greenlet in each of the monsters, and dozens of greenlet in a room are normal. Summed up, is python+gevent in order to circumvent the global lock, the use of multi-process, each process more than Greenlet implementation mode.

And for Golang, will become the bottom is a multithreaded model, the above running multiple goroutine, when a goroutine launched the blocking IO, the bottom of the goroutine to run the thread, you can block the wait. The system then opens a new thread, picking a stack of goroutine to execute. Golang is equivalent to multithreading, replacing the previous PY multi-process.

After reading the cloud-wind blog, my understanding of Skynet is that each thread runs a LUA VM and a series of VMS form a queue. Then each VM has its own message queue, and when the VM runs, it takes a message from its own message queue to execute. Thanks to the LUA sandbox mechanism, even if a VM has traceback, it can be effectively isolated. At the same time, because of the same process, the data interaction between VMS can be quite efficient, such as strings of the transfer as long as the pointer.

However, I have previously overlooked the role of the LUA association in this process. Originally, a service (that is, a VM) received a package req, it will create a new process to handle. During processing, if an external call request is generated, the Req packet is treated as finished, and the current process is scratched, waiting for the call request to return to wake up. The service can then continue to create new processes to process new message packets without blocking them. In other words, if a skynet.call call occurs during the process of processing a package, it can cause multiple concurrent execution. If you do not pay attention to using locks to protect shared resources between threads, problems may occur.

Skynet of the process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.