This is a creation in Article, where the information may have evolved or changed.
The idea is simple. By setting runtime.GOMAXPROCS(1)
the Golang process into single-threaded execution. Similar to Python with gevent effect. asynchronous I/O concurrency is then implemented by scheduling multiple threads. PHP runs in the go process as a sub-function, and PHP needs to yield to other threads, through callbacks to the Golang function. When you invoke the child function provided by go from PHP, go guarantees that the current context of PHP is saved. The original PHP context is restored when the co-process execution is allowed to return. The key code is:
//Save the PHP context on the current thread Oldserverctx: = engine. Servercontextget () fmt. Println (oldserverctx) defer engine. Servercontextset (oldserverctx) Oldexecutorctx: = engine. Executorcontextget () fmt. Println (oldexecutorctx) defer engine. Executorcontextset (oldexecutorctx) Oldcorectx: = engine. Corecontextget () fmt. Println (oldcorectx) defer engine. Corecontextset (oldcorectx) //discards the global lock so that other threads can start executing PHP Enginelock.unlock () defer Enginelock.lock ()
Servercontextget These functions are I add, get the PHP (EG/SG/PG) These three global context (see: http://www.cnblogs.com/chance ... ). Modified Php-go source code in: https://github.com/taowen/go-...
Demo of the complete Php/go hybrid process:
PackageMainImport("FMT" "Github.com/deuill/go-php/engine" "OS" "Runtime" "Time" "Sync")typeTestobjstruct{} func newtestobj(args []interface{}) interface {} {return&testobj{}}varEnginelock *sync. Mutex func (self *testobj) Hello() {oldserverctx: = engine. Servercontextget () fmt. Println (OLDSERVERCTX)deferEngine. Servercontextset (oldserverctx) Oldexecutorctx: = engine. Executorcontextget () fmt. Println (OLDEXECUTORCTX)deferEngine. Executorcontextset (oldexecutorctx) Oldcorectx: = engine. Corecontextget () fmt. Println (OLDCORECTX)deferEngine. Corecontextset (OLDCORECTX) Enginelock.unlock ()deferEnginelock.lock () time. Sleep (time. Second) fmt. Println ("Sleep Done")} func main() {Runtime. Gomaxprocs (1) Theengine, err: = engine. New () Enginelock = &sync. mutex{}ifErr! =Nil{FMT. PRINTLN (Err)} _, Err = Theengine.define ("Testobj", newtestobj) WG: = &sync. waitgroup{} WG. ADD (2) Before: = time. Now () Fmt. Println ("1")Go func() {Enginelock.lock ()deferEnginelock.unlock () Context1, err: = Theengine.newcontext ()ifErr! =Nil{FMT. PRINTLN (Err)} context1. Output = OS. StdoutifErr! =Nil{FMT. PRINTLN (Err)} FMT. Println ("1 Enter") _, Err = Context1. Eval ("$TESTOBJ = new Testobj (); $TESTOBJ->hello (); ") FMT. Println ("1 Back")ifErr! =Nil{FMT. PRINTLN (ERR)}//theengine.destroycontext (CONTEXT1)Fmt. Println ("1 Done") WG. Done ()} () Fmt. Println ("2")Go func() {Enginelock.lock ()deferEnginelock.unlock () context2, err: = Theengine.newcontext ()ifErr! =Nil{FMT. PRINTLN (ERR)}ifErr! =Nil{FMT. PRINTLN (Err)} context2. Output = OS. Stdout FMT. Println ("2 Enter") _, Err = Context2. Eval ("$TESTOBJ = new Testobj (); $TESTOBJ->hello (); ") FMT. Println ("2 Back")ifErr! =Nil{FMT. PRINTLN (ERR)}//theengine.destroycontext (CONTEXT2)Fmt. Println ("2 Done") WG. Done ()} () WG. Wait () After: = time. Now () Fmt. Println (after. Sub (before))