Skip to content

Commit

Permalink
Support PyPy 3.7 officially and auto-test it in CI
Browse files Browse the repository at this point in the history
PyPy might give 2-4x speedup on pure-Python scripts and save memory by default (as if slots were used in CPython). This enabled Kubernetes operators in CPU-/RAM-tight environments without special improvements and optimizations from Kopf's side. PyPy was unofficially supported and tested manually from time to time. With this change, let's make it official and automated.

MyPy is excluded from test-time dependencies: one of MyPy's sub-dependency ("typed-ast") fails at being installed (python/typed_ast#111). However, PyPy is not used for linting/type-checking, only CPython is. Therefore, MyPy is not needed for PyPy.

Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
  • Loading branch information
nolar committed Oct 10, 2021
1 parent a3d9888 commit 1e58ad5
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
fail-fast: false
matrix:
install-extras: [ "", "full-auth" ]
python-version: [ "3.7", "3.8", "3.9", "3.10" ]
python-version: [ "3.7", "3.8", "3.9", "3.10", "pypy-3.7" ]
name: Python ${{ matrix.python-version }} ${{ matrix.install-extras }}
runs-on: ubuntu-20.04
timeout-minutes: 5 # usually 2-3 mins
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/thorough.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
fail-fast: false
matrix:
install-extras: [ "", "full-auth" ]
python-version: [ "3.7", "3.8", "3.9", "3.10" ]
python-version: [ "3.7", "3.8", "3.9", "3.10", "pypy-3.7" ]
name: Python ${{ matrix.python-version }} ${{ matrix.install-extras }}
runs-on: ubuntu-20.04
timeout-minutes: 5 # usually 2-3 mins
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ That easy! For more features, see the [documentation](https://kopf.readthedocs.i

## Usage

Python 3.7+ is required:
[CPython](https://www.python.org/) and [PyPy](https://www.pypy.org/)
are officially supported and tested; other Python implementations can work too.

We assume that when the operator is executed in the cluster, it must be packaged
into a docker image with a CI/CD tool of your preference.

Expand Down
4 changes: 4 additions & 0 deletions docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Installation

.. highlight:: bash

Prerequisites:

* Python >= 3.7 (CPython and PyPy are officially tested and supported).

To install Kopf::

pip install kopf
Expand Down
4 changes: 3 additions & 1 deletion kopf/_kits/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ async def _serve_fn(request: aiohttp.web.Request) -> aiohttp.web.Response:
runner = aiohttp.web.AppRunner(app, handle_signals=False)
await runner.setup()
try:
# Note: reuse_port is mostly (but not only) for fast-running tests with SSL sockets;
# multi-threaded sockets are not really used -- high load is not expected for webhooks.
addr = self.addr or None # None is aiohttp's "any interface"
port = self.port or self._allocate_free_port()
site = aiohttp.web.TCPSite(runner, addr, port, ssl_context=context)
site = aiohttp.web.TCPSite(runner, addr, port, ssl_context=context, reuse_port=True)
await site.start()

# Log with the actual URL: normalised, with hostname/port set.
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ freezegun
import-linter
isort>=5.5.0
lxml
mypy==0.910
# Mypy requires typed-ast, which is broken on PyPy 3.7 (could work in PyPy 3.8).
mypy==0.910; implementation_name == "cpython"
pre-commit
pyngrok
pytest>=6.0.0
Expand Down
5 changes: 5 additions & 0 deletions tests/reactor/test_queueing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""
import asyncio
import contextlib
import gc
import weakref

import async_timeout
Expand Down Expand Up @@ -224,6 +225,10 @@ async def test_garbage_collection_of_streams(settings, stream, events, unique, w
# The jobs can take a tiny moment more, but this is noticeable in the tests.
await asyncio.sleep(0.1)

# For PyPy: force the gc! (GC can be delayed in PyPy, unlike in CPython.)
# https://doc.pypy.org/en/latest/cpython_differences.html#differences-related-to-garbage-collection-strategies
gc.collect()

# Truly garbage-collected? Memory freed?
assert all([ref() is None for ref in refs])

Expand Down

0 comments on commit 1e58ad5

Please sign in to comment.