What are some strategies to write python code that works in CPython, Jython and IronPython

我与影子孤独终老i 提交于 2019-12-02 20:45:59

If you do find you need to write unique code for an environment, use pythons

import mymodule_jython as mymodule

import mymodule_cpython as mymodule

have this stuff in a simple module (''module_importer''?) and write your code like this:

from module_importer import mymodule

This way, all you need to do is alter module_importer.py per platform.

@Daren Thomas: I agree, but you should use the platform module to determine which interpreter you're running.

I write code for CPython and IronPython but tip should work for Jython as well.

Basically, I write all the platform specific code in separate modules/packages and then import the appropriate one based on platform I'm running on. (see cdleary's comment above)

This is especially important when it comes to the differences between the SQLite implementations and if you are implementing any GUI code.

The #1 thing IMO: Focus on thread safety. CPython's GIL makes writing threadsafe code easy because only one thread can access the interpreter at a time. IronPython and Jython are a little less hand-holding though.

I'm pretty sure you already know this but unfortunately Jython can't load c extension modules.

There are two major issues at play here...

Firstly, to my knowledge, only CPython has RAII - you have to close your own resources in Jython, Ironpython, etc.

And Secondly, as has been mentioned, is thread safety.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!