Skip to content

Commit 1e58ad5

Browse files
committed
Support PyPy 3.7 officially and auto-test it in CI
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 <[email protected]>
1 parent a3d9888 commit 1e58ad5

File tree

7 files changed

+20
-4
lines changed

7 files changed

+20
-4
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
fail-fast: false
3939
matrix:
4040
install-extras: [ "", "full-auth" ]
41-
python-version: [ "3.7", "3.8", "3.9", "3.10" ]
41+
python-version: [ "3.7", "3.8", "3.9", "3.10", "pypy-3.7" ]
4242
name: Python ${{ matrix.python-version }} ${{ matrix.install-extras }}
4343
runs-on: ubuntu-20.04
4444
timeout-minutes: 5 # usually 2-3 mins

.github/workflows/thorough.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
fail-fast: false
4343
matrix:
4444
install-extras: [ "", "full-auth" ]
45-
python-version: [ "3.7", "3.8", "3.9", "3.10" ]
45+
python-version: [ "3.7", "3.8", "3.9", "3.10", "pypy-3.7" ]
4646
name: Python ${{ matrix.python-version }} ${{ matrix.install-extras }}
4747
runs-on: ubuntu-20.04
4848
timeout-minutes: 5 # usually 2-3 mins

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ That easy! For more features, see the [documentation](https://kopf.readthedocs.i
124124

125125
## Usage
126126

127+
Python 3.7+ is required:
128+
[CPython](https://www.python.org/) and [PyPy](https://www.pypy.org/)
129+
are officially supported and tested; other Python implementations can work too.
130+
127131
We assume that when the operator is executed in the cluster, it must be packaged
128132
into a docker image with a CI/CD tool of your preference.
129133

docs/install.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Installation
44

55
.. highlight:: bash
66

7+
Prerequisites:
8+
9+
* Python >= 3.7 (CPython and PyPy are officially tested and supported).
10+
711
To install Kopf::
812

913
pip install kopf

kopf/_kits/webhooks.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,11 @@ async def _serve_fn(request: aiohttp.web.Request) -> aiohttp.web.Response:
160160
runner = aiohttp.web.AppRunner(app, handle_signals=False)
161161
await runner.setup()
162162
try:
163+
# Note: reuse_port is mostly (but not only) for fast-running tests with SSL sockets;
164+
# multi-threaded sockets are not really used -- high load is not expected for webhooks.
163165
addr = self.addr or None # None is aiohttp's "any interface"
164166
port = self.port or self._allocate_free_port()
165-
site = aiohttp.web.TCPSite(runner, addr, port, ssl_context=context)
167+
site = aiohttp.web.TCPSite(runner, addr, port, ssl_context=context, reuse_port=True)
166168
await site.start()
167169

168170
# Log with the actual URL: normalised, with hostname/port set.

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ freezegun
1414
import-linter
1515
isort>=5.5.0
1616
lxml
17-
mypy==0.910
17+
# Mypy requires typed-ast, which is broken on PyPy 3.7 (could work in PyPy 3.8).
18+
mypy==0.910; implementation_name == "cpython"
1819
pre-commit
1920
pyngrok
2021
pytest>=6.0.0

tests/reactor/test_queueing.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""
1717
import asyncio
1818
import contextlib
19+
import gc
1920
import weakref
2021

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

228+
# For PyPy: force the gc! (GC can be delayed in PyPy, unlike in CPython.)
229+
# https://doc.pypy.org/en/latest/cpython_differences.html#differences-related-to-garbage-collection-strategies
230+
gc.collect()
231+
227232
# Truly garbage-collected? Memory freed?
228233
assert all([ref() is None for ref in refs])
229234

0 commit comments

Comments
 (0)