Pygoridge: Python-to-Golang IPC bridge, python client for Goridge
Pygoridge 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 excellent Goridge library.
The library allows you to call Go service methods from Python with minimal footprint, structures and []byte support.
Also Pygoridge includes Worker class to use in worker processes with https://github.com/spiral/roadrunner - high-performance application server, load-balancer and process manager written in Golang.
Features
- no external dependencies
- can be used with RoadRunner to 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 pygoridge
Example: python client calls go server methods
from pygoridge import create_relay, RPC, SocketRelay
rpc = RPC(SocketRelay("127.0.0.1", 6001))
# or, using factory
tcp_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 manager
with RPC(tcp_relay) as rpc:
print(rpc("App.Hi", "Antony, again"))
package main
import (
"fmt"
"github.com/spiral/goridge"
"net"
"net/rpc"
)
type App struct{}
func (s *App) Hi(name string, r *string) error {
*r = fmt.Sprintf("Hello, %s!", name)
return nil
}
func main() {
ln, err := net.Listen("tcp", ":6001")
if err != nil {
panic(err)
}
rpc.Register(new(App))
for {
conn, err := ln.Accept()
if err != nil {
continue
}
go rpc.ServeCodec(goridge.NewCodec(conn))
}
}
Example: go http server (RoadRunner) with python workers
You can download latest RoadRunner binary from releases page.
See also examples.
cd examples/roadrunner/http_server/
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
Worker class
from functools import partial
import ujson
from pygoridge import create_relay, Worker
json_dumps = partial(
ujson.dumps, ensure_ascii=False,
escape_forward_slashes=False)
json_loads = ujson.loads
class HTTPWorker(Worker):
def hello(self, headers):
return headers, {"X-Server": "RoadRunner with python workers"}
if __name__ == "__main__":
rl = create_relay("pipes")
worker = HTTPWorker(rl, json_encoder=json_dumps, json_decoder=json_loads)
while True:
context, body = worker.receive()
if context is None:
continue
http_headers = json_loads(context.tobytes())
response, response_headers = worker.hello(http_headers)
worker.send(
json_dumps(response).encode("utf-8"),
response_headers
)
Run RoadRunner server
cd examples/roadrunner/http_server/
./rr serve -d -v
Make http request to get request headers back as a response body
curl 'http://localhost:8080/' --compressed
RoadRunner is highly customizable and extendable so you can even write your own plugin for it with required API protocol (see for example php-grpc server).
Custom encoders/decoders for faster json processing
from pygoridge.json import json_dumps, json_loads
# you can also provide custom json encoder for faster marshalling
rpc = RPC(tcp_relay, json_encoder=json_dumps, json_decoder=json_loads)
License
The MIT License (MIT). Please see LICENSE for more information.
Development
Run tests
docker-compose -f ./goridge/tests/docker-compose.yml up -d
docker-compose -f tests/rr_test_app/docker-compose.yml up -d
python3 -m unittest discover -s tests
Run linter
pip install flake8
flake8 pygoridge
Metadata
Release files for pygoridge 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pygoridge-0.1.0.tar.gz | 10.2 kB | Without provenance | Details for pygoridge-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pygoridge-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for pygoridge-0.1.0-py3-none-any.whl |
Total release size: 21.2 kB
Release files / pygoridge-0.1.0.tar.gz
| Download URL | pygoridge-0.1.0.tar.gz |
|---|---|
| Size | 10.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b4927aa95b81d0d234f3da52ace982143523bf234c8a64c614f7a8eacfc84390
|
|
BLAKE2b-256 checksum How to use checksums |
83a2310526cf5d81b6934dfe6b5c8d044b3e5a0b3af76dcc3ff130d604dd8c8b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.2
|
Release files / pygoridge-0.1.0-py3-none-any.whl
| Download URL | pygoridge-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
43bc4026dde7484d949fc87626285f3e9866ff10aa68636bc9c8032c113f0775
|
|
BLAKE2b-256 checksum How to use checksums |
64386eee904b03b666b6bb7f32efc44794c46c0204d99cea06ae11828b2219bd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.2
|
Security for pygoridge 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.