V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Gem
V2EX  ›  Python

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

  •  
  •   Gem · 2017-01-05 07:31:30 +08:00 · 3902 次点击
    这是一个创建于 2672 天前的主题,其中的信息可能已经有所发展或是发生改变。
    目标:

    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
    30 条回复    2017-01-06 00:43:26 +08:00
    j5shi
        1
    j5shi  
       2017-01-05 07:35:31 +08:00 via iPhone
    所以相当于一个编译器而不是解释器?
    loading
        2
    loading  
       2017-01-05 07:38:20 +08:00 via Android
    但是我用了 flask ,能搞定?
    wweir
        3
    wweir  
       2017-01-05 07:41:15 +08:00 via Android
    @j5shi 源码翻译工具。

    Python 在运维等几个场景很有优势,
    go 在效率和部署方面有很大优势,
    这二者结合,感觉有戏。

    不爽的是 Google 弃坑的习俗……
    j5shi
        4
    j5shi  
       2017-01-05 07:45:32 +08:00 via iPhone
    @wweir 我知道是源码翻译工具,但最终翻译成了 native code ,所以是二进制机器码?如果是,那就成了编译器了,速度应该会有显著提升
    wweir
        5
    wweir  
       2017-01-05 08:11:49 +08:00
    @j5shi 所以你觉得是 python 的编译工具喽?
    抱歉,并不是。如果是的话,又和 go 有什么关系呢
    bearqq
        6
    bearqq  
       2017-01-05 08:17:07 +08:00 via Android
    看见 go 第一反应已经是围棋了→_→
    试试对二进制 c 扩展的库支持
    echo1937
        7
    echo1937  
       2017-01-05 08:59:31 +08:00
    也有好处,运维脚本直接往服务器上一丢就行了,不用费心环境了。

    当然前提是 Google 管生又管养,毕竟这不是 Go 这种大项目。
    congeec
        8
    congeec  
       2017-01-05 09:03:04 +08:00
    那为嘛不用 Nuikta 编译到 C++呢?
    janxin
        9
    janxin  
       2017-01-05 09:08:58 +08:00 via iPhone
    @congeec 你还需要编译环境,尤其是版本复杂的时候
    WildCat
        10
    WildCat  
       2017-01-05 09:18:59 +08:00
    这个翻译器还不如 j2objc ,完全没可读性:
    https://gist.github.com/imWildCat/b1c1caaa4e6258759c7748fb36a5a744
    j5shi
        11
    j5shi  
       2017-01-05 09:28:18 +08:00 via iPhone
    @wweir 那你觉得这玩意儿的主要目地是什么?
    congeec
        12
    congeec  
       2017-01-05 09:31:17 +08:00
    @janxin 编译成 Go 也需要编译环境啊。‘版本复杂的时候’ 是啥意思?不好意思没看懂,有点懵
    est
        13
    est  
       2017-01-05 09:43:23 +08:00
    坐等有人搞出个 decorator

    比如:


    @grumpy
    def aaa():
    pass
    wweir
        14
    wweir  
       2017-01-05 09:52:05 +08:00 via Android
    @j5shi 说实话,这问题我十分不乐意回复,然而,强迫症……

    我的第一个回复里面讲得很清楚了
    j5shi
        15
    j5shi  
       2017-01-05 10:13:21 +08:00 via iPhone
    @wweir 正是因为看了你的第一个回复和第二个回复,我才觉得你说的非常有问题,你说不需要编译,那我问你如果把 python 翻译成 go 而速度得到提升,你觉得是什么原因?你懂编译原理不?
    kappa
        16
    kappa  
       2017-01-05 10:35:59 +08:00
    @janxin 跟版本什么关系?
    ```
    tools/grumpc hello.py > hello.go
    go build -o hello hello.go
    ```
    只是把 Python 源码翻译成 Go 的
    j5shi
        17
    j5shi  
       2017-01-05 10:43:35 +08:00 via iPhone
    @kappa 其实翻译不是终极目地,终极目的是用 go 的编译器
    janxin
        18
    janxin  
       2017-01-05 10:57:16 +08:00
    @congeec
    @kappa
    我是说 Nuikta 编译到 C++,如果遇到系统版本 /libc 版本复杂的时候就不好玩了,这个在一个多年公司里也是比较常见的情况。
    编译成 Go 可以编译一次到处运行嘛。
    aljun
        19
    aljun  
       2017-01-05 11:17:53 +08:00
    - -我本还一直想拿 golang 给 py 写个 wsgi (结果一直没时间。。。。。看来。。。可以拿这个撸撸
    mathgl
        20
    mathgl  
       2017-01-05 11:58:53 +08:00
    @congeec nuitka 这玩意有人在生产环境用过不?几乎找不到什么案例。
    jjx
        21
    jjx  
       2017-01-05 12:06:35 +08:00
    这个东西要成功的前提是 先提出一个语言规范. 规范什么能做什么不能做

    否则要兼容 cpython, jython/ironpython/pypy/pyston 这么多年现在都在前面踩坑呢
    fy
        22
    fy  
       2017-01-05 12:38:49 +08:00
    这多半只是 Google 内部自用的老代码迁移转换器,不是给你在生产环境用的。
    pwder
        23
    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
        24
    jyf  
       2017-01-05 13:03:24 +08:00
    正打算用这个试试看可能提升下 falcon 框架
    zeroten
        25
    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
        26
    yongzhong  
       2017-01-05 13:18:39 +08:00
    @zeroten naive
    zeroten
        27
    zeroten  
       2017-01-05 13:26:23 +08:00
    @yongzhong 所以突然感觉我也好 naive 。。。。既然这样,那中间代码是 go 还是 C 不都一样么。。。干脆叫 python 编译器。。。
    yongzhong
        28
    yongzhong  
       2017-01-05 13:32:39 +08:00
    @zeroten 以我的理解,主要的作用还是规避 GIL 和部署更方便吧.单从运行效率来看,不一定会高很多
    wwqgtxx
        29
    wwqgtxx  
       2017-01-05 20:10:01 +08:00
    @zeroten 如果他不这么做,那大量依赖 python 动态语言特性的 lib 不都报废了,看着是效率高了,实际上这个项目也就没有存在的意义了
    heimeil
        30
    heimeil  
       2017-01-06 00:43:26 +08:00
    目前发现三点:
    一、将 python 翻译成 Go 的时候会把线程翻译成 goroutine ,这样线程就非常轻量了。
    二、 python 里面可以直接调用 Go 的标准库, from __go__.net.http import ListenAndServe , python 的库就更丰富了。
    三、翻译成 Go ,可以用 Go 的跨平台编译特性,直接编译成不同平台的二进制文件,易于分发。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   896 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 22:33 · PVG 06:33 · LAX 15:33 · JFK 18:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.