跳转到主要内容

Gothon可以在Python中运行GO代码,只需从Python导入*.go文件。

项目描述

# Gothon

使用纯Go编写的Python 3模块。无需CFFI,无需CTypes,无需CGo,无需PyBindGen,只需从Python导入`*.go`文件。

它使用IPC RPC工作。受[Cython的`pyximport`](https://cython-docs.pythonlang.cn/en/latest/src/tutorial/cython_tutorial.html?highlight=pyximport#pyximport-cython-compilation-for-developers)和[CPPImport](https://github.com/tbenthompson/cppimport#import-c-or-c-files-directly-from-python)的启发。设计用于通过Go加速Python。

任何希望贡献的人都可以访问开源仓库,只需联系我。**这是一个正在进行中的项目。**

https://unsplash.com/@stickermule的说明性照片


# 使用

```python
$ ls
gothon.py python_module.go

$ python
Python 3.6.4 (default, Jan 5 2018, 02:35:40)

>>> import gothon # 导入并启用Gothon。
>>> gothon.import_hook()
>>> import python_module # 导入*.go文件。
>>> repr(python_module)
'<Gothon对象 140066220608272 from python_module.go>'
>>> python_module.__doc__
"Gothon使用IPC RPC JSON在Python中运行GO代码。"
>>> worker = python_module.start()
>> worker.call("Echo.Echo", "Hello from Python to Go")
'Hello from Python to Go'
>>>
```


安装

```
pip install gothon
```
- 使用pip卸载`pip uninstall gothon`


文档

<details>
<summary><b>gothon.py</b></summary>

**描述:**
Gothon使用IPC RPC JSON从Python运行GO代码。

因为从基准测试来看,它比TCP/UDP套接字快3倍,所以使用Unix套接字。

这不会连接到网络、互联网,也不使用HTTP。

将解析、编译、构建和缓存委托给Go本身。

此项目面向开发者,而不是最终用户。

此项目可以与Fades、FireJails、Docker、RKT一起使用。

此项目假设至少有非常基本的Go编程语言知识。

如果您需要在您的项目上集成它,请随时联系我们。

**参数:**
- `go_file` 要编译和作为Python模块运行的GO文件,字符串类型,默认为`python_module.go`,必需。
- `startup_delay` 启动延迟,在构建go文件后,但在将IPC RPC返回给Python之前,浮点类型,默认为`0.1`,可选。

**关键字参数:** 无。

**返回值:** `gothon.RPCJSONClient()` 一个自定义的IPC RPC。

**基类:** `object`。

**类型:** `object`。

**源代码文件:** https://github.com/juancarlospaco/gothon/blob/master/gothon.py

| 状态 | 系统 | 描述 |
| ------------------ |:-----------:| -----------:|
| :white_check_mark: | **Linux** | 正常工作 |
| :white_check_mark: | **Os X** | 正常工作 |

**用法示例:**

```python
>>> 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()
>>>
```

</details>


需求

- [Python 3.6+] (https://pythonlang.cn)
- [Go 1.10+] (https://golang.ac.cn)


##### 故障排除

<details>
<summary>无法工作</summary>

- 删除所有`__pycache__`和`*.pyc`。
- 执行`go clean -x -cache`(通常Go会自动处理缓存清理)。
- 将Go更新到最新版本。
</details>


## 为什么

我尝试过但无效的事情(2018,Linux)

- https://blog.filippo.io/building-python-modules-with-go-1-5/(仅Go 1.5)
- https://github.com/go-python/gopy/issues/83(仅Python 2)
- https://github.com/go-python/gopy/blob/master/gen.go#L81(仅Python 2)
- https://github.com/sbinet/go-python(仅Python 2)
- https://dustymabe.com/2016/09/13/sharing-a-go-library-to-python-using-cffi/(编译器在嵌入式混淆C时出错)
- http://pybindgen.readthedocs.io/en/latest/tutorial/#supported-python-versions(编译器在混淆C时出错)
- https://hackernoon.com/extending-python-3-in-go-78f3a69552ac(所有上述内容)
- 其他解决方案是从Go调用Python,而不是Gothon。
- 其他我不记得链接。


### 贡献者

**请在Github上星标此仓库**!,这有助于更快地出现在搜索结果中。
- [帮助](https://help.github.com/articles/using-pull-requests) 和更多 [帮助](https://help.github.com/articles/fork-a-repo) 和交互式快速 [Git教程](https://try.github.io)。
- 英语是主要默认使用的语言,除非明确说明 otherwise *(例如,不要发送翻译拉取请求)*
- 欢迎接受能够通过unittests的working的拉取请求。


### 许可证

- BSD。


### 道德和人文政策

- 宗教是不允许的。贡献意味着你同意COC。

项目详情


下载文件

下载适合您的平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

gothon-0.5.0.zip (13.1 kB 查看哈希值)

上传时间

构建分布

gothon-0.5.0-py3.6.egg (7.1 kB 查看哈希值)

上传时间

gothon-0.5.0-py2.py3-none-any.whl (10.0 kB 查看哈希值)

上传时间 Python 2 Python 3

支持