Grumpy: Go running Python, python 很得 “人和”,对,说的是 CPython2.7

2017-01-05 07:31:30 +08:00
 Gem
目标:

Grumpy is a Python to Go source code transcompiler and runtime that is intended to be a near drop in replacement for CPython 2.7.



原理: python sc -> | Grumpy | -> go sc -> native

The key difference is that it compiles Python source code to Go source code which is then compiled to native code, rather than to bytecode. This means that Grumpy has no VM. The compiled Go source code is a series of calls to the Grumpy runtime, a Go library serving a similar purpose to the Python C API (although the C API is not directly supported).



关于库:

Python 标准库和 Grumpy 库分为两部分,具体看下面

Python 标准库 : Much of the Python standard library is written in Python and so it "just works" in Grumpy. These parts of the standard library are copied from CPython 2.7 (possibly with light modifications). For licensing reasons, these files are kept in the third_party/stdlib subdir.

The parts of the standard library that cannot be written in pure Python, e.g. file and directory operations, are kept in the lib subdir. In CPython these kinds of modules are written as C extensions. In Grumpy they are written in Python but they use native Go extensions to access facilities not otherwise available in Python.


关于Python3

看样子,貌似是后续可能会支持,或者单独fork一个python3的版本。


仓库地址:
https://github.com/google/grumpy
4062 次点击
所在节点    Python
30 条回复
jjx
2017-01-05 12:06:35 +08:00
这个东西要成功的前提是 先提出一个语言规范. 规范什么能做什么不能做

否则要兼容 cpython, jython/ironpython/pypy/pyston 这么多年现在都在前面踩坑呢
fy
2017-01-05 12:38:49 +08:00
这多半只是 Google 内部自用的老代码迁移转换器,不是给你在生产环境用的。
pwder
2017-01-05 12:55:10 +08:00
https://opensource.googleblog.com/2017/01/grumpy-go-running-python.html

"But the biggest advantage is that interoperability with Go code becomes very powerful and straightforward: Grumpy programs can import Go packages just like Python modules!"
jyf
2017-01-05 13:03:24 +08:00
正打算用这个试试看可能提升下 falcon 框架
zeroten
2017-01-05 13:11:43 +08:00
额,这翻译出来的东西。。。
按照官方的教程。。。

python 代码: print "hello, world"翻译成了
```go
package main
import (
πg "grumpy"
π_os "os"
)
func initModule(πF *πg.Frame, _ []*πg.Object) (*πg.Object, *πg.BaseException) {
var πTemp001 []*πg.Object
_ = πTemp001
var πE *πg.BaseException; _ = πE
for ; πF.State() >= 0; πF.PopCheckpoint() {
switch πF.State() {
case 0:
default: panic("unexpected function state")
}
// line 1: print "hello, world"
πF.SetLineno(1)
πTemp001 = make([]*πg.Object, 1)
πTemp001[0] = πg.NewStr("hello,\x20world").ToObject()
if πE = πg.Print(πF, πTemp001, true); πE != nil {
continue
}
return nil, nil
}
return nil, πE
}
var Code *πg.Code
func main() {
Code = πg.NewCode("<module>", "hello.py", nil, 0, initModule)
π_os.Exit(πg.RunMain(Code))
}

```
我还以为会翻译成 fmt.Println("hello,world")
yongzhong
2017-01-05 13:18:39 +08:00
@zeroten naive
zeroten
2017-01-05 13:26:23 +08:00
@yongzhong 所以突然感觉我也好 naive 。。。。既然这样,那中间代码是 go 还是 C 不都一样么。。。干脆叫 python 编译器。。。
yongzhong
2017-01-05 13:32:39 +08:00
@zeroten 以我的理解,主要的作用还是规避 GIL 和部署更方便吧.单从运行效率来看,不一定会高很多
wwqgtxx
2017-01-05 20:10:01 +08:00
@zeroten 如果他不这么做,那大量依赖 python 动态语言特性的 lib 不都报废了,看着是效率高了,实际上这个项目也就没有存在的意义了
heimeil
2017-01-06 00:43:26 +08:00
目前发现三点:
一、将 python 翻译成 Go 的时候会把线程翻译成 goroutine ,这样线程就非常轻量了。
二、 python 里面可以直接调用 Go 的标准库, from __go__.net.http import ListenAndServe , python 的库就更丰富了。
三、翻译成 Go ,可以用 Go 的跨平台编译特性,直接编译成不同平台的二进制文件,易于分发。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/332296

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX