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

前端 未结 6 1915
忘了有多久
忘了有多久 2021-02-02 14:55

Having tries to target two of these environments at the same time I can safely say the if you have to use a database etc. you end up having to write unique code for that environ

相关标签:
6条回答
  • 2021-02-02 15:05

    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.

    0 讨论(0)
  • 2021-02-02 15:05

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

    0 讨论(0)
  • 2021-02-02 15:05

    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.

    0 讨论(0)
  • 2021-02-02 15:13

    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.

    0 讨论(0)
  • 2021-02-02 15:20

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

    0 讨论(0)
  • 2021-02-02 15:26

    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.

    0 讨论(0)
提交回复
热议问题