简体   繁体   English

Python:在一个项目中使用支持不同Python版本的模块

[英]Python: Using modules which support different Python versions in one project

I have 2 python modules where one only supports Python 2.x and the other one 3.x. 我有2个python模块,其中一个仅支持Python 2.x,另一个仅支持3.x。 Unfortunately I need both for a project. 不幸的是,我需要一个项目。 My workaround for now is to have them run on their own as separate programs and building up their communication via the socket module. 我现在的解决方法是让它们作为单独的程序独立运行,并通过套接字模块建立通信。

  1. I will be ending up with 2 executables, what I would like to avoid. 我将最终得到2个可执行文件,这是我想避免的。
  2. The "connection" between both modules has to be as fast as possible. 两个模块之间的“连接”必须尽可能快。

So my question is if there is a way to somehow combine both to one executable at the end and if there is a better solution for a fast communication as the client-server construction I have now. 所以我的问题是,是否有一种方法可以将两者最终合并到一个可执行文件中,以及是否有一种更好的解决方案,如我现在拥有的客户端-服务器结构那样,可以进行快速通信。

There really is no good way to avoid that workaround. 确实没有避免该变通办法的好方法。

Conceptually, there's no reason that you couldn't embed two interpreters into the same process. 从概念上讲,没有理由不能将两个解释器嵌入到同一过程中。 But practically, the CPython interpreter depends on some static/global state. 但是实际上,CPython解释器依赖于某些静态/全局状态。 While 3.7 is much better about that than, say, 3.0 or 2.6 was, that state still hasn't nearly been eliminated. 尽管3.7比3.0或2.6更好,但该状态仍未几乎被消除。 1 And, the way C linkage works, there's no way to get around that without changing the interpreter. 1而且,C链接的工作方式是,不更改解释器就无法解决该问题。

Also, embedding CPython isn't hard , but it's not trivial , in the way that running an interpreter as a subprocess is trivial—and it may be harder than coming up with an efficient way to pass or share state between subprocesses. 此外,嵌入的CPython并不 ,但它不是小事 ,在方式运行的解释作为子是微不足道的,而且它可能更难比想出一个有效的方式来传递或子流程之间共享状态。

Of course there are other interpreters besides CPython. 当然,除了CPython,还有其他解释器。 But the other major implementation with both 2.7 and 3.x versions isn't easily embeddable (PyPy), and the two that are easily embeddable don't have 3.x versions, and also can only be embedded in another VM, and can't run C extension modules (Jython and IronPython). 但是,同时具有2.7和3.x版本的其他主要实现并不容易嵌入(PyPy),而易于嵌入的两个实现都没有3.x版本,并且只能嵌入到另一个VM中,并且可以不要运行C扩展模块(Jython和IronPython)。 It is possible to do something like using JEP to embed CPython 3.7 via JNI in a JVM while also using Jython 2.7 natively in that same JVM, but I doubt that approach will work for you. 可能的,而在同一JVM还用Jython 2.7本身在JVM通过JNI做类似的东西用JEP嵌入CPython的3.7,但我怀疑这种方法会为你工作。

Meanwhile, I mentioned that passing or sharing data between processes generally isn't that hard. 同时,我提到在进程之间传递或共享数据通常并不难。

  • If you don't have that much data, you can usually just pass it pickled over a pipe. 如果没有太多数据,通常可以将其腌制后通过管道传递。
  • If you do have a ton of data, it usually is, or could be, stored in memory in some structured form—numpy arrays, big hunks of ASCII or UTF-8 text, arrays of ctypes structs, etc.—that you can overlay on an mmap or shared memory segment. 如果您确实有大量数据,通常可以或可以将它们以某种结构化形式存储在内存中,例如numpy数组,大块ASCII或UTF-8文本, ctypes结构数组等,可以覆盖在mmap或共享内存段上。
  • Or, of course, you can come up with your own protocol and communicate with it over a (UNIX or IP) socket. 或者,当然,您可以提出自己的协议,并通过(UNIX或IP)套接字与之通信。 But you don't necessarily have to jump right to that option. 但是您不必一定要跳到该选项。

Notice that multiprocessing supports both of the first two—although to take advantage of it with independent interpreters, you have to dig into its source and pull out the bits you need. 请注意, multiprocessing支持前两者,尽管要利用独立的解释器来利用它,但您必须挖掘其源并提取所需的位。 And there are also third-party libraries that can help. 此外,还有第三方库可以提供帮助。 (For example, if you need to pickle things that don't pickle natively, the answer is often as simple as "replace pickle with dill ".) (例如,如果您需要腌制不是本国腌制的东西,答案通常很简单,例如“用dill代替pickle ”。)


1. Running multiple subinterpreters in various restricted ways does sort of work with things like mod_wsgi , and PEP 554 aims to get things to the state where you can easily and cleanly run multiple 3.7 subinterpreters in the same process, but still nothing like completely independent embeddings of CPython—the subinterpreters share a GIL, a cycle collector, an atexit handler, etc. 1.以各种受限方式运行多个子解释器可对mod_wsgi类的东西进行某种处理,而PEP 554旨在使状态达到可以在同一过程中轻松,干净地运行多个3.7子解释器的状态,但仍不像完全独立的嵌入那样的CPython语言—子解释器共享一个GIL,一个循环收集器,一个atexit处理程序等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM