Python 3 modules written on pure Go. No CFFI, no CTypes, no CGo, no PyBindGen, just import *.go
files from Python.
I stoped this project, I changed Go for NIM that uses Python syntax, is faster, smaller binaries, compiles to C and JS.
It works using RPC. Inspired by Cythons pyximport
and CPPImport. Designed to speed up Python using Go.
Open Repo access to anyone who want to contribute, just contact me.
$ ls
gothon.py python_module.go
$ python
Python 3.6.4 (default, Jan 5 2018, 02:35:40)
>>> import gothon # Import & enable Gothon.
>>> gothon.import_hook()
>>> import python_module # Import *.go files.
>>> repr(python_module)
'<Gothon object 140066220608272 from python_module.go>'
>>> python_module.__doc__
'Gothon runs GO Code from Python using IPC RPC JSON.'
>>> worker = python_module.start()
>>> worker.call("Echo.Echo", "Hello from Python to Go")
'Hello from Python to Go'
>>>
pip install gothon
- No dependencies, it does not install any Python dependency nor Go dependency.
- Uninstall
pip uninstall gothon
gothon.Gothon()
Description: Gothon runs GO Code from Python using IPC RPC JSON.
Delegates the Parse, Compile, Build and Cache to Go itself.
If you Upgrade your Go version you dont have to change anything on Gothon, it just works.
If you Upgrade your Python version you dont have to change anything on Gothon, it just works.
Unix Socket are used because from benchmarks it performs 3x faster than TCP/UDP Sockets.
This does not connect to the network, nor internet, nor use HTTP.
This project is oriented to Developers, NOT end-users.
This project can be used with Fades, FireJails, Docker, RKT.
This project assumes at least very basic knowledge of the Go programming language.
Its recommended to have 1 *.go
file importable from Python for project or package,
the *.go
file itself can import Go functions from other *.go
files using Go way of importing stuff.
Feel free to contact us if you need help integrating it on your project.
Arguments:
go_file
A GO file to compile and run as a python module,str
orpathlib.Path
type, defaults topython_module.go
, required.startup_delay
A startup delay, after building the go file but before returning the IPC RPC to Python, float type, defaults to0.1
, can be set to0.0
too, can not beNone
, optional.
Keyword Arguments: None.
Returns: gothon.RPCJSONClient()
an custom IPC RPC.
Base Class: object
.
Type: object
.
Source Code file: https://github.com/juancarlospaco/gothon/blob/master/gothon.py
State | OS | Description |
---|---|---|
✅ | Linux | Works Ok |
✅ | Os X | Works Ok |
Usage Example:
>>> from gothon import Gothon
>>> unemployed = Gothon()
>>> worker = unemployed.start()
>>> worker.call("Echo.Echo", "Hello from Python to Go")
'Hello from Python to Go'
>>> worker.stop()
>>>
Helper Static Methods:
gothon.Gothon().template()
Prints to standard output a Go source code template to start hacking into, with all bits and pieces to write a Python module using Go, it has 1 "Echo" function that you can overwrite or delete, this Go source code is ready to run as-is.
gothon.Gothon().clean()
Clean up the Cache, uses glob.iglob()
and pathlib.Path().unlink()
, its very fast.
Not working
- Delete all
__pycache__
and*.pyc
. - Execute
go clean -x -cache
(Usually Go takes care of cleaning Cache automatically). - Update your Go to the latest version.
Things that Ive tried and didnt work (2018, Linux):
- https://blog.filippo.io/building-python-modules-with-go-1-5/ (Go 1.5 only)
- go-python/gopy#83 (Python 2 only)
- https://github.com/go-python/gopy/blob/master/gen.go#L81 (Python 2 only)
- https://github.com/sbinet/go-python (Python 2 only)
- https://dustymabe.com/2016/09/13/sharing-a-go-library-to-python-using-cffi/ (Compiler Errors on embebed obfuscated C)
- http://pybindgen.readthedocs.io/en/latest/tutorial/#supported-python-versions (Compiler Errors on obfuscated C)
- https://hackernoon.com/extending-python-3-in-go-78f3a69552ac (All of the above)
- https://medium.com/learning-the-go-programming-language/calling-go-functions-from-other-languages-4c7d8bcc69bf (Go 1.5 only)
- Other solutions call Python from Go, the reverse of Gothon.
- Others I dont remember the links. No KISS solution. Both languages have simplicity but other solutions have error prone complexity, while this may not be ideal, I feel is better than manual C glue code or inlined C embebed CFFI hacks.
- This project welcomes all Python and Go developers.
- Please Star this Repo on Github !, it helps to show up faster on searchs.
- Help and more Help and Interactive Quick Git Tutorial.
- English is the primary default spoken language, unless explicitly stated otherwise (eg. Dont send Translation Pull Request)
- Pull Requests for working passing unittests welcomed.
- BSD.
- Religions is not allowed. Contributing means you agree with the COC.