In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
The analysis of the use of golua virtual machine, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Preface
I always wanted to do a distributed process system like openflow, but I did a half-project because of the time and application scenario, and I thought it was a bit simple. I thought that as long as some students were willing, there should be no problem in development, but in the end, there were problems with project management and development capabilities, and finally ran aground. But I have always wanted to do a distributed process scheduling system. In fact, I have done a flow system with another classmate in the company, and I have used it online, and it is still in use today. I wanted to optimize this system some time ago, and the goal is to make a small and available open source version. After a period of carding has been initially completed, the background + front-end code re-carding has also been completed.
Use of lua virtual machine
Today, I mainly want to write about a technical point used in it: lua. In the internal version, we use golang development, but in order to ensure that the processes do not interact with each other, we use lua virtual machine technology; each process is executed in a separate lua virtual machine. The internal version uses golua as a component, but there is a problem with this component, which is actually a version of cgo, that is, it actually calls the local lua library to execute, and there are some efficiency problems in compilation, deployment and execution.
So I want to use other methods in the newly developed version, after studying for a period of time, I decided to use gopher-lua, which is a virtual machine that supports Lua5.1 using golang rewriting, which can be seamlessly combined with golang directly in execution, has a very good api interface for go, and is much smoother in use. I also use some ideas of golang in the implementation, and it is better to combine with golang. After learning a handful of lua this time, I found that this technology is really powerful. According to statistics, the c implementation code of lua is only about 1w lines, but the execution is very efficient, and there are very few reserved keywords in lua. It can be said to be a very concise and efficient language.
Some characteristics of gopher-lua
It is very suitable for my flow project, so I replaced golua with gopher-lua. Because the api and type support of gopher-lua is much better than golua, I have streamlined a lot of code after the replacement. Here are some of the key points used in the project.
Creation and use of lua virtual machine
Api is very simple to use, with the following steps:
Introduction of gopher-lua
Create a virtual machine
Use a virtual machine to execute lua statements or lua script files
Shut down the virtual machine
Package mainimport lua "github.com/yuin/gopher-lua" / / 1. Introduce gopher-luafunc main () {L: = lua.NewState () / / 2. Create an instance of the lua interpreter defer L.Close () / / 4. Shut down the virtual machine if err: = L.DoString (`print ("hello") `); err! = nil {/ / 3. Execute the lua statement / / if err: = L.DoFile ("hello.lua") with the created virtual machine; err! = nil {/ / 3. Execute the lua script file panic (err)}} with the created virtual machine
Api is still very simple to use. The rest on basic data types is not covered here, but is described in great detail on its github site.
The following will focus on two very useful function points used in flow projects.
Call lua function in go
For a service written in golang, what if we want to use the functions defined in the lua script? Gopher-lua provides a response method, and there are very good examples on its site to illustrate:
First load the lua script with the DoFile method, and define the lua function required in the script
Secondly, use the CallByParam function to make the call
L: = lua.NewState () defer L.Close () if err: = L.DoFile ("double.lua") Err! = nil {panic (err)} if err: = L.CallByParam (lua.P {Fn: L.GetGlobal ("double"), / / get the reference NRet: 1 of the double function, / / specify the number of returned values Protect: true, / / if an exception occurs, panic or err}, lua.LNumber (10)) Err! = nil {/ / pass input parameter: 10 panic (err)} ret: = L.Get (- 1) / / get the returned result value L.Pop (1) / / throw the returned result from the stack
The function call of GopherLua is made through the stack. The parameters that need to be passed to the function are pressed into the stack before the call, and the result is put on the stack after the function execution is completed. The caller executes the result by taking the function at the top of the stack.
Call go function in lua script
Go calls the lua function relatively less in the flow project, using the following: call the go function in the lua script; because many complex operations its practical lua to do is still a bit complex and unsafe, especially some common operations or complex db operations. So a good practice is to package the function in go, and then write the lua script externally, and then call the go function when executing, which can not only meet the flexibility of the lua script, but also greatly expand the ability of lua and reduce the complexity of writing.
First take a look at its basic usage, which is also illustrated by an example on the official website.
Func Double (L * lua.LState) int {lv: = L.ToInt (1) / / get seven parameters L.Push (lua.LNumber (lv * 2)) / / stack the results on return 1 / / return the number of returned parameters} func main () {L: = lua.NewState () defer L.Close () L.SetGlobal ("double") L.NewFunction (Double)) / / Registration function}
First of all, there is a type lua.LGFunction in gopher-lua, which is a function type, which fixes the input and output parameters of the function, the input parameter is a reference to lua.LState, and the return value is an int. As defined below, if you need more parameters, you need to use the stack or extend the members to the lua.LState. After the execution is completed, the return value is also passed out through the stack or to the lua.LState extension member.
There are many ways to register a function, one of which is mentioned above, and another way I saw on the Internet is to use lua's table.
Myfuns: = L.NewTable () L.SetGlobal ("myfuns", myfuns) / / register function L.SetField (myfuns, "gofun1", L.NewFunction (gofun1)). / / call method err: = L.DoString (`test = myfuns:gofun1 ("test") `)
One of the inconvenient points is that the transfer and acquisition of parameters is not very intuitive, and it is often necessary to call functions in two separate packages. This is a little inconvenient. Everything else is fine.
GopherLua can create a very clean instance of the Lua interpreter without loading any system modules. The module provided by the programmer is then registered to provide a secure sandbox environment for embedded scripts.
This is the answer to the question about the analysis of the use of the golua virtual machine. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.