如何让 PyPy、Django 和 PostgreSQL 协同工作?

发布于 2025-01-06 20:57:49 字数 787 浏览 4 评论 0原文

应该使用什么分支或包组合来使 PyPy、Django 和 PostgreSQL 很好地协同工作?

我知道 PyPy 和 Django 可以很好地配合,但我对 PyPy 和 PostgreSQL 不太确定。我确实看到 Alex Gaynor 制作了一个名为 pypy-postgresql 的 PyPy 分支。我也知道有些人正在使用 psycopg2-ctypes

这些叉子有区别吗?或者我们应该使用稳定的 1.9 PyPy 并使用 psycopg2-ctypes?使用 ctypes 选项可能会损害性能,请参阅下面的评论。

另外,有人在使用 PyPy 和 pyscopg2 时遇到过任何陷阱吗?如果出现问题,使用 CPython 似乎很容易,但我主要是在寻找程序员可以提前做好准备的事情。

我环顾四周,似乎 psycopg2 本身并不能与 PyPy 一起工作。虽然 psycopg2-ctypes 似乎确实对某些人有用,但在 pypy-dev。我在 Windows 上工作,遗憾的是,我认为 psycopg2-ctypes 还没有为 Windows 做好准备。

What fork, or combination of packages should one to use to make PyPy, Django and PostgreSQL play nice together?

I know that PyPy and Django play nice together, but I am less certain about PyPy and PostgreSQL. I do see that Alex Gaynor has made a fork of PyPy called pypy-postgresql. I also know that some people are using psycopg2-ctypes.

Is there a difference between these forks? Or should we use the stable 1.9 PyPy and use psycopg2-ctypes? Using the ctypes options could hurt performance, see the comment below.

Also, has anyone experienced any pitfalls with using PyPy with pyscopg2? It seems easy enough to fall back on CPython if something isn't working right, but mostly I'm looking for things a programmer can do ahead of time to prepare.

I looked around, it doesn't seem that psycopg2 works natively with PyPy. Although, psycopg2-ctypes does seem to be working for some people, there was a discussion on pypy-dev. I work on Windows, and I don't think psycopg2-ctypes is ready for Windows yet, sadly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮世清欢 2025-01-13 20:57:49

psycopg2cffi(2015 年更新)

psycopg2cffi 是另一个与 psycopg2 兼容的替代品,应该可以通过 PyPy 提供最佳的 PostgreSQL 性能。将其添加到您的 settings.py 以保持与两者兼容:

try:
    import psycopg2
except ImportError:
    # Fall back to psycopg2cffi
    from psycopg2cffi import compat
    compat.register()

psycopg2-ctypes (2012)

我也知道有些人正在使用 psycopg2-ctypes。

这是最简单的方法;为了保持两者兼容,只需在 Django settings.py 中添加此代码:

try:
    import psycopg2
except ImportError:
    # Fall back to psycopg2-ctypes
    from psycopg2ct import compat
    compat.register()

我在几个版本前测试了此代码;遗憾的是,根据我的经验,psycopg2-ctypes 否定了 PyPy 带来的微小性能提升。但是,YMMV,这取决于您的代码总体上对 JIT 友好程度以及您实际花费在运行 Python 代码上的时间比例。也许 PyPy 从那时起就有所改进。

我认为 psycopg2-ctypes 还没有为 Windows 做好准备

我还没有尝试过,但 ctypes 是平台无关的。 AFAICT 您只需确保 libpq.dll 库是可加载的(位于 PATH 环境变量或本地目录中的目录中)并且它应该在 Windows 上工作,就像在 Linux 中一样。

pypy-postgresql

我确实看到 Alex Gaynor 制作了一个名为 pypy-postgresql 的 PyPy 分支。

我认为从长远来看这不是一个好的选择。该分支已经一年多没有更新了,我构建它的尝试失败了。无论如何,在解释器中硬编码 PostgreSQL 驱动程序似乎是错误的。

我相信 pypy-postgresql 也没有二进制文件,所以如果你想使用它,你需要自己构建整个 PyPy 分支。不适合胆小的人:需要数十分钟且机器至少有 4 GB 内存。 (官方说明:http://pypy.org/download.html#building-from-source

要构建,您首先需要源代码。如果您安装了 Mercurial,则只需 hg 克隆 https://bitbucket.org/alex_gaynor/pypy-postgresql 即可。如果没有,您可以下载自动“tip”zip 文件: https://bitbucket.org/alex_gaynor/ pypy-postgresql/get/tip.zip

打开命令行,进入解压后的目录,然后进入 pypy/translator/goal

如果你有 PyPy安装后,建议使用它来构建:

pypy translate.py -Ojit

否则:

python translate.py -Ojit

遗憾的是我的知识就到此为止了。我收到错误“BytecodeCorruption:未实现的操作码,ofs=234,code=203,name=BUILD_LIST_FROM_ARG

psycopg2cffi (Updated 2015)

psycopg2cffi is yet another psycopg2-compatible replacement and should provide the best PostgreSQL performance with PyPy. Add this to your settings.py to remain compatible with both:

try:
    import psycopg2
except ImportError:
    # Fall back to psycopg2cffi
    from psycopg2cffi import compat
    compat.register()

psycopg2-ctypes (2012)

I also know that some people are using psycopg2-ctypes.

This is the easiest way; to stay compatible with both, just add this code in your Django settings.py:

try:
    import psycopg2
except ImportError:
    # Fall back to psycopg2-ctypes
    from psycopg2ct import compat
    compat.register()

I tested this a few releases ago; sadly in my experience, psycopg2-ctypes negates the small performance gains afforded by PyPy. But YMMV, it depends on how JIT-friendly your code is in general and what fraction of time you actually spend running Python code. And maybe PyPy has just improved since then.

and I don't think psycopg2-ctypes is ready for Windows yet

I haven't tried this, but ctypes is platform-independent. AFAICT you just have to make sure that the libpq.dll library is loadable (located in a directory in your PATH environment variable or local directory) and it should work on Windows just like in Linux.

pypy-postgresql

I do see that Alex Gaynor has made a fork of PyPy called pypy-postgresql.

I don't think this is a good choice in the long term. The branch hasn't been updated for more than a year and my attempts to build it have failed. And it seems wrong to hard-code a PostgreSQL driver in the interpreter anyway.

I believe there are no binaries out there of pypy-postgresql either, so if you want to use it, you'd need to build the whole PyPy branch yourself. Not for the faint of heart: it takes tens of minutes and a machine with at least 4 GB of memory. (Official instructions: http://pypy.org/download.html#building-from-source)

To build, you first need the source. If you have Mercurial installed, you can simply hg clone https://bitbucket.org/alex_gaynor/pypy-postgresql. If not, you can download the automagic "tip" zip file: https://bitbucket.org/alex_gaynor/pypy-postgresql/get/tip.zip

Open a command line, go into the decompressed directory, and then inside pypy/translator/goal

If you have PyPy installed, it's recommended to use that for building:

pypy translate.py -Ojit

Otherwise:

python translate.py -Ojit

Sadly this is where my knowledge ends. I get the error "BytecodeCorruption: unimplemented opcode, ofs=234, code=203, name=BUILD_LIST_FROM_ARG"

擦肩而过的背影 2025-01-13 20:57:49

一些其他资源:

  • PyPy 兼容性信息: 数据库适配器
  • Python wiki
  • psycopg2cffi 上的 PostgreSQL 页面,作者:Konstantin Lopuhin:
    PyPy 2.0 及更新版本的基于 cffi 的 psycopg2 实现
    博客文章GitHub 存储库PyPI 页面pypy-dev 线程
    – 这看起来是目前最强的候选者,但我还没有测试过它
  • psycopg2ct by Michael van Tellingen:
    PyPy 1.6 及更高版本的 psycopg2 基于 ctypes 的实现
    GitHub 存储库PyPI页
  • pypy-postgresql 作者:Alex Gaynor:
    废弃的 psycopg2 的 RPython 端口实现为 PyPy 的分支(Bitbucket repo
  • pypq
    “使用 ctypes 和 libpq.so 的 Python PostgreSQL DBAPI 2.0 兼容驱动程序,可与 PyPy 配合使用”
    讨论PyPI 页面
  • bpgsql
    “Barebones 纯 python PostGreSQL 客户端。主要符合 DB-API 2.0 (PEP 249) 要求。包括实验性 Django 1.0 后端”
    讨论网页Google 代码页
  • pg8000
    “与 PostgreSQL 数据库引擎兼容的 DB-API 2.0 纯 Python 接口 [...] 不依赖于任何外部库(例如编译的 python 模块或 PostgreSQL 的 libpq 库)”
    网页GitHub 存储库PyPI 页面

Some additional resources:

  • PyPy compatibility information: DB adaptors
  • PostgreSQL page on the Python wiki
  • psycopg2cffi by Konstantin Lopuhin:
    cffi based implementation of psycopg2 for PyPy 2.0 and newer
    (blog post, GitHub repo, PyPI page, pypy-dev thread)
    – this looks like the strongest candidate currently, but I haven't tested it yet
  • psycopg2ct by Michael van Tellingen:
    ctypes based implementation of psycopg2 for PyPy 1.6 and newer
    (GitHub repo, PyPI page)
  • pypy-postgresql by Alex Gaynor:
    abandoned RPython port of psycopg2 implemented as a fork of PyPy (Bitbucket repo)
  • pypq:
    "Python PostgreSQL DBAPI 2.0 compliant driver using ctypes and libpq.so, works with PyPy"
    (discussion, PyPI page)
  • bpgsql:
    "Barebones pure-python PostGreSQL client. Mostly DB-API 2.0 (PEP 249) compliant. Includes an experimental Django 1.0 backend"
    (discussion, web page, Google Code page)
  • pg8000:
    "a DB-API 2.0 compatible Pure-Python interface to the PostgreSQL database engine [...] does not rely on any external libraries (such as a compiled python module, or PostgreSQL’s libpq library)"
    (web page, GitHub repo, PyPI page)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文