从CPython迁移到Jython [英] Migrating from CPython to Jython

查看:93
本文介绍了从CPython迁移到Jython的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑将我的代码(大约30K LOC)从CPython移到Jython,以便可以更好地与Java代码集成.

I'm considering moving my code (around 30K LOC) from CPython to Jython, so that I could have better integration with my java code.

是否有我应该查看的清单或指南来帮助我进行迁移?有人有做类似事情的经验吗?

Is there a checklist or a guide I should look at, to help my with the migration? Does anyone have experience with doing something similar?

通过阅读 Jython网站,大多数问题似乎太晦涩难懂我.

From reading the Jython site, most of the problems seem too obscure to bother me.

我确实注意到了:

  • 线程安全是个问题
  • Unicode支持似乎大不相同,这可能对我来说是个问题
  • mysqldb无法正常工作,需要用zxJDBC替换

还有什么?

相关问题:推荐答案

我将从其他答案和我的经验中收集的wiki形式开始.随时编辑和添加内容,但请尝试遵循实用建议,而不要列出损坏的内容.这是来自Jython网站的旧的差异列表.

I'm starting this as a wiki collected from the other answers and my experience. Feel free to edit and add stuff, but please try to stick to practical advice rather than a list of broken things. Here's an old list of differences from the Jython site.

Jython不使用引用计数,因此资源在释放时就被释放了. 被垃圾收集了,这要晚得多,您会看到 CPython程序

Jython does not use reference counting, and so resources are released as they are garbage collected, which is much later then you'd see in the equivalent CPython program

  • open('file').read()不会自动关闭文件. 最好使用with open('file') as fp习惯用法.
  • 在Jython代码中很晚才调用__ del __方法,而不是立即调用 在删除对该对象的最后一个引用之后.
  • open('file').read() doesn't automatically close the file. Better use the with open('file') as fp idiom.
  • The __ del __ method is invoked very late in Jython code, not immediately after the last reference to the object is deleted.

mysqldb是c模块,因此无法在jython中工作.相反,你 应该使用Jython随附的com.ziclix.python.sql.zxJDBC.

mysqldb is a c module, and therefore will not work in jython. Instead, you should use com.ziclix.python.sql.zxJDBC, which comes bundled with Jython.

替换以下MySQLdb代码:

Replace the following MySQLdb code:

connection = MySQLdb.connect(host, user, passwd, db, use_unicode=True, chatset='utf8')

使用:

url = "jdbc:mysql://%s/%s?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull" % (host, db)
connections = zxJDBC.connect(url, user, passwd, "com.mysql.jdbc.Driver")

您还需要将所有_mysql_exception替换为zxJDBC.

You'll also need to replace all _mysql_exception with zxJDBC.

最后,您需要将查询占位符从%s替换为?.

Finally, you'll need to replace the query placeholders from %s to ?.

  • 您不能在Jython中表达非法的unicode字符.尝试一些 像unichr(0xd800)会导致异常,并且具有文字u'\ud800' 在您的代码中只会造成严重破坏.
  • You can't express illegal unicode characters in Jython. Trying something like unichr(0xd800) would cause an exception, and having a literal u'\ud800' in your code will just wreak havoc.
    当然,
  • C模块不可用.
    • C modules are not available, of course.
      • So no NumPy or SciPy.
      • 对于大多数工作负载,Jython将比CPython慢​​得多.报告是 慢3到50倍之间的任何事情.
      • For most workloads, Jython will be much slower than CPython. Reports are anything between 3 to 50 times slower.

      Jython项目仍然有效,但是发展并不快.这 开发邮件列表 每月大约有20条消息,而且似乎只有大约2个开发人员 最近提交代码.

      The Jython project is still alive, but is not fast-moving. The dev mailing list has about 20 messages a month, and there seem to be only about 2 developers commiting code lately.

      这篇关于从CPython迁移到Jython的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆