Migrating from CPython to Jython(从 CPython 迁移到 Jython)
问题描述
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?
From reading the Jython site, most of the problems seem too obscure to bother me.
I did notice that:
- thread safety is an issue
- Unicode support seems to be quite different, which may be a problem for me
- mysqldb doesn't work and needs to be replaced with zxJDBC
Anything else?
Related question: What are some strategies to write python code that works in CPython, Jython and IronPython
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.
Resource management
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()
doesn't automatically close the file. Better use thewith 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.
MySQL Integration
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.
Replace the following MySQLdb code:
connection = MySQLdb.connect(host, user, passwd, db, use_unicode=True, chatset='utf8')
With:
url = "jdbc:mysql://%s/%s?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull" % (host, db)
connections = zxJDBC.connect(url, user, passwd, "com.mysql.jdbc.Driver")
You'll also need to replace all _mysql_exception
with zxJDBC
.
Finally, you'll need to replace the query placeholders from %s
to ?
.
Unicode
- You can't express illegal unicode characters in Jython. Trying something
like
unichr(0xd800)
would cause an exception, and having a literalu'ud800'
in your code will just wreak havoc.
Missing things
- C modules are not available, of course.
- So no NumPy or SciPy.
- os.spawn* functions are not implemented. Instead use subprocess.call.
Performance
- For most workloads, Jython will be much slower than CPython. Reports are anything between 3 to 50 times slower.
Community
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 CPython 迁移到 Jython


基础教程推荐
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01