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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9350422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:39:21  来源:igfitidea点击:

How do you get PyPy, Django and PostgreSQL to work together?

pythondjangopostgresqlpsycopg2pypy

提问by James R

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

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

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.

我知道 PyPy 和 Django 一起玩得很好,但我不太确定 PyPy 和 PostgreSQL。我确实看到 Alex Gaynor 制作了一个名为pypy-postgresql的 PyPy分支。我也知道有些人正在使用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.

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

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.

另外,有没有人在 pyscopg2 中使用 PyPy 时遇到过任何陷阱?如果某些事情不能正常工作,则退回到 CPython 似乎很容易,但主要是我在寻找程序员可以提前做的准备工作。

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.

我环顾四周,似乎 psycopg2 本身并不适用于 PyPy。尽管 psycopg2-ctypes 似乎确实对某些人有用,但对pypy-dev进行了讨论。我在 Windows 上工作,但遗憾的是,我认为 psycopg2-ctypes 还没有为 Windows 做好准备。

采纳答案by intgr

psycopg2cffi (Updated 2015)

psycopg2cffi(2015 年更新)

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

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 (2012)

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

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

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

这是最简单的方法;要与两者保持兼容,只需在您的 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.

我在几个版本前对此进行了测试;遗憾的是,根据我的经验,psycopg2-ctypes 抵消了 PyPy 提供的小幅性能提升。但是 YMMV,这取决于您的代码通常对 JIT 的友好程度以及您实际花在运行 Python 代码上的时间。也许 PyPy 从那时起才有所改进。

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

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

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

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

pypy-postgresql

pypy-postgresql

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

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

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.

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

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)

我相信 pypy-postgresql 也没有二进制文件,所以如果你想使用它,你需要自己构建整个 PyPy 分支。不适合胆小的人:它需要数十分钟和至少 4 GB 内存的机器。(官方说明: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

要构建,您首先需要源代码。如果您安装了 Mercurial,您只需hg clone https://bitbucket.org/alex_gaynor/pypy-postgresql. 如果没有,您可以下载自动“tip”压缩文件: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

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

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

如果您安装了 PyPy,建议使用它来构建:

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"

可悲的是,这就是我的知识结束的地方。我收到错误“ BytecodeCorruption: unimplemented opcode, ofs=234, code=203, name=BUILD_LIST_FROM_ARG

回答by akaihola

Some additional resources:

一些额外的资源:

  • PyPy compatibility information: DB adaptors
  • PostgreSQL pageon the Python wiki
  • psycopg2cffiby 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
  • psycopg2ctby Michael van Tellingen:
    ctypes based implementation of psycopg2 for PyPy 1.6 and newer
    (GitHub repo, PyPI page)
  • pypy-postgresqlby 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)
  • PyPy 兼容性信息:DB 适配器
  • Python wiki 上的PostgreSQL 页面
  • psycopg2cffi由康斯坦丁Lopuhin:
    基于CFFI实施psycopg2对PyPy 2.0和更高版本
    博客文章GitHub库的PyPI页pypy-dev的线程
    -这看起来像目前最强的候选人,但我还没有测试它尚未
  • psycopg2ctby Michael van Tellingen:
    PyPy 1.6 和更新版本的 psycopg2 基于 ctypes 的实现
    GitHub 存储库PyPI 页面
  • pypy-postgresqlby Alex Gaynor:
    废弃的 psycopg2 的 RPython 端口作为 PyPy 的一个分支实现(Bitbucket repo
  • pypq
    “使用 ctypes 和 libpq.so 的 Python PostgreSQL DBAPI 2.0 兼容驱动程序,适用于 PyPy”
    讨论PyPI 页面
  • bpgsql
    “准系统纯 python PostGreSQL 客户端。主要是 DB-API 2.0 (PEP 249) 兼容。包括一个实验性的 Django 1.0 后端”
    讨论网页谷歌代码页
  • pg8000
    “PostgreSQL 数据库引擎的 DB-API 2.0 兼容 Pure-Python 接口 [...] 不依赖任何外部库(例如编译的 python 模块,或 PostgreSQL 的 libpq 库)”
    网页GitHub 存储库), PyPI 页面)