Skip to main content

Pygoridge: Python-to-Golang IPC bridge, python client for Goridge

GoDoc

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)

Source distribution for pygoridge 0.1.0
File Size Uploaded
pygoridge-0.1.0.tar.gz 10.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pygoridge 0.1.0
File Interpreter ABI Platform
pygoridge-0.1.0-py3-none-any.whl Python 3 none any Details

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

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page