Package: pygoridge
Pygoridge: Python-to-Golang IPC bridge, python client for GoridgePygoridge is a Python-to-Golang codec library which works over sockets and Golang net/rpc package.
This is a python port of php client for an excellentGoridgelibrary.
The library allows you to call Go service methods from Python with minimal footprint, structures and[]bytesupport.Also Pygoridge includesWorkerclass to use in worker processes withhttps://github.com/spiral/roadrunner- high-performance application server, load-balancer and process manager written in Golang.Featuresno external dependenciescan be used withRoadRunnerto create CPU-intensive servers with simple sequential python workers (no multiprocessing module required). This is really helpful to overcome GIL.Installation$ go get "github.com/spiral/goridge"$ pip install pygoridgeExample: python client calls go server methodsfrompygoridgeimportcreate_relay,RPC,SocketRelayrpc=RPC(SocketRelay("127.0.0.1",6001))# or, using factorytcp_relay=create_relay("tcp://127.0.0.1:6001")unix_relay=create_relay("unix:///tmp/rpc.sock")stream_relay=create_relay("pipes")print(rpc("App.Hi","Antony"))rpc.close()# close underlying socket connection# or using as a context managerwithRPC(tcp_relay)asrpc:print(rpc("App.Hi","Antony, again"))packagemainimport("fmt""github.com/spiral/goridge""net""net/rpc")typeAppstruct{}func(s*App)Hi(namestring,r*string)error{*r=fmt.Sprintf("Hello, %s!",name)returnnil}funcmain(){ln,err:=net.Listen("tcp",":6001")iferr!=nil{panic(err)}rpc.Register(new(App))for{conn,err:=ln.Accept()iferr!=nil{continue}gorpc.ServeCodec(goridge.NewCodec(conn))}}Example: go http server (RoadRunner) with python workersYou can download latest RoadRunner binary fromreleases page.See alsoexamples.cdexamples/roadrunner/http_server/
python3-mvenv.venvsource.venv/bin/activate
pipinstall-rrequirements.txtWorker classfromfunctoolsimportpartialimportujsonfrompygoridgeimportcreate_relay,Workerjson_dumps=partial(ujson.dumps,ensure_ascii=False,escape_forward_slashes=False)json_loads=ujson.loadsclassHTTPWorker(Worker):defhello(self,headers):returnheaders,{"X-Server":"RoadRunner with python workers"}if__name__=="__main__":rl=create_relay("pipes")worker=HTTPWorker(rl,json_encoder=json_dumps,json_decoder=json_loads)whileTrue:context,body=worker.receive()ifcontextisNone:continuehttp_headers=json_loads(context.tobytes())response,response_headers=worker.hello(http_headers)worker.send(json_dumps(response).encode("utf-8"),response_headers)Run RoadRunner servercdexamples/roadrunner/http_server/
./rrserve-d-vMake http request to get request headers back as a response bodycurl'http://localhost:8080/'--compressedRoadRunner is highly customizable and extendable so you can even write your own plugin for it with required API protocol (see for examplephp-grpc server).Custom encoders/decoders for faster json processingfrompygoridge.jsonimportjson_dumps,json_loads# you can also provide custom json encoder for faster marshallingrpc=RPC(tcp_relay,json_encoder=json_dumps,json_decoder=json_loads)LicenseThe MIT License (MIT). Please seeLICENSEfor more information.DevelopmentRun testsdocker-compose-f./goridge/tests/docker-compose.ymlup-d
docker-compose-ftests/rr_test_app/docker-compose.ymlup-d
python3-munittestdiscover-stestsRun linterpip install flake8
flake8 pygoridge