Commit graph

860 commits

Author SHA1 Message Date
Tobias Ahrens
302a96d84d
Update documentation to async code (#331) (#332)
* Update documentation to async code (#331)

This commit updates the documentation to the latest changes added
with pycapnp 2.0.0.

* Remove non existing classes/functions from the reference documentation
* Adapt the quickstart to the latest changes. Mainly to new rpc handling,
  that now exlusively is done through asyncio.

* DOC: Add section about send and receive messages over a socket

Since #313 it is possible to read and write messages over a socket.
This commit adds a small section for read and write in the quickstart.
2023-10-16 11:05:27 -07:00
Lasse Blaauwbroek
1446386636 Make a server fail early when the KJ loop is not running 2023-10-15 08:10:54 -07:00
Lasse Blaauwbroek
cc088211dc Delete and update some Python 3.7-specific todo notes
When I wrote some of these, I was wrong about the affected versions. Upgraded
them to v3.9
2023-10-13 07:55:57 -07:00
Lasse Blaauwbroek
b22763f3c6 Fix 'AttributeError: '_UnixSelectorEventLoop' object has no attribute 'call_soon'
See haata/pycapnp#1 for a discussion. The cause of this bug is still unknown to
me. But it likely has been fixed in Python 3.10. For some crazy reason, you can
just keep retrying the offending call, and the attribute will magically
'reappear'.
2023-10-13 06:47:25 -07:00
Lasse Blaauwbroek
ae965128de
Update README example to async code (#331) 2023-10-12 07:13:27 -07:00
Tobias Ahrens
941f018092
Fix broken test in test_load (#329)
The new context manager for the event loop
seems to be missing in `test_load_capnp`.
2023-10-11 11:33:41 -07:00
Tobias Ahrens
bb15822850
Fixes for capnp 1.0 (#1)
* add capnp_api.h to gitignore

* Change type of read_min_bytes from size to int

Not sure why this was not causing issues before or if that
is the right fix ... but it seems to be fine :)

* Adapt python_requires to >=3.8

This was overlooked when 3.7 was deprecated. The ci no longer
works with python 3.7 and cibuildwheel uses python_requires ...

* Replace deprecated find_module with find_spec (importlib)

find_module was deprecated with python 3.4 and python 3.12
removed it (https://docs.python.org/3.12/whatsnew/3.12.html#importlib).

The new command is find_spec and only required a few adaptions
2023-10-11 11:28:24 -07:00
Jacob Alexander
313d0d4c6d Prepare for v2.0.0b1 release
- Update CHANGELOG.md
- Update to bundled capnproto-1.0.1
  * Compiles with capnproto-0.8.0 and higher
- *Breaking Change* Remove allow_cancellation (see
  https://capnproto.org/news/2023-07-28-capnproto-1.0.html)
  * This is tricky to handle for older versions of capnproto. Instead of
    dealing with lots of complication, removing it entirely.
- Fix some documentation after the build backend support was added
- Update tox.ini to support 3.8 to 3.12
- Update cibuildwheel to 2.16.1
  * Adds Python 3.12 supports and implicitly deprecates EOL 3.7 (though it's
    still built)
2023-10-03 12:29:48 -07:00
Lasse Blaauwbroek
e13a0c9254
Experiment: Wrap all capnp code in a context-manager to avoid segfaults (#317)
* Experiment: Wrap all capnp code in a context-manager

* Fix segfault in on_disconnect
2023-10-03 09:04:51 -07:00
Tobias Ahrens
d48ffea939
Add custom build backend to support build args (#328)
This implements a custom build backend, inspired by the
[solution used by Pillow.](https://github.com/python-pillow/Pillow/pull/7171)

install-option is deprecated and was removed with pip 23.1. The
comonly accepted solution seems to be to define a custom build
backend for now
https://github.com/pypa/setuptools/issues/2491#issuecomment-1589764230

This commit changes the usage from `--install-option` to `--config-settings`.
2023-10-02 22:30:51 -07:00
Karl
db26d60283
Pin cython to below version 3, Python 3.11 support (#320)
* Pin cython to below version 3

Cython 3 includes backwards incompatible changes so it's no longer
possible to install pycapnp from source.

* Add py311 environment

I'm not sure if this is necessary, but 3.11 is out so might as well?
2023-07-21 01:40:49 -07:00
Jacob Alexander
8f3bfc3536
Merge pull request #307 from Zentren/dynamic_schema_loading
Schema loading from the wire
2023-06-19 13:27:15 -07:00
DaneSlattery
f59b3fdc91 adjust tests for async 2023-06-19 12:21:38 +02:00
Rowan Reeve
a5c29a74d2 Schema loading from the wire
Cap'n Proto provides a schema loader, which can be used to dynamically
load schemas during runtime. To port this functionality to pycapnp,
a new class is provided `C_SchemaLoader`, which exposes the Cap'n
Proto C++ interface, and `SchemaLoader`, which is part of the pycapnp
library.

The specific use case for this is when a capnp message contains
a Node.Reader: The schema for a yet unseen message can be loaded
dynamically, allowing the future message to be properly processed.

If the message is a struct containing other structs, all the schemas for
every struct must be loaded to correctly parse the message. See
https://github.com/DaneSlattery/capnp_generic_poc for a
proof-of-concept.

Add docs and cleanup

Add more docs

Reduce changes

Fix flake8 formatting

Fix get datatype
2023-06-12 14:10:13 +02:00
Jacob Alexander
b439993b1f
Merge pull request #315 from LasseBlaauwbroek/cull-sync
Remove the synchronous RPC mode
2023-06-12 00:31:16 -07:00
Lasse Blaauwbroek
1e94f2e321 Remove the option to create servers through _new_server.
Inheritance is required now to create a server
2023-06-11 04:09:03 +02:00
Lasse Blaauwbroek
5edf700548 Remove .capnp fixture from test_capability_context.py
Using a fixture makes things more complicated. If we want to test explicit
capnp.load() functionality, we can do that separately
2023-06-11 04:09:01 +02:00
Lasse Blaauwbroek
95bb528ea2 Delete test_capability_old.py, which is mostly redundant
All of these tests also exist in test_capability.py. The only difference is the
way the .capnp file is loaded. But that could be tested with much less code.
2023-06-11 03:51:23 +02:00
Lasse Blaauwbroek
83d610c116 Remove some more c++ helper functions 2023-06-11 03:50:54 +02:00
Lasse Blaauwbroek
0483596da1 Miscellaneous 2023-06-09 22:05:12 +02:00
Lasse Blaauwbroek
d6261b6d79 Manually handle deallocation of some objects for the benefit of p3.7
Python 3.7 seems to have trouble dealocating objects in a timely fashion. We
rely on this, because the c++ destructors need to run before the kj event loop
is closed. Hence, we do it manually.
2023-06-09 22:05:12 +02:00
Lasse Blaauwbroek
74ebaff4e3 Make older python versions work 2023-06-09 22:05:05 +02:00
Lasse Blaauwbroek
0d160fc81d Fix forgotten async server method 2023-06-09 22:04:39 +02:00
Lasse Blaauwbroek
84d0f365ad Fix formatting 2023-06-08 08:18:50 +02:00
Lasse Blaauwbroek
bc01774ede Add pytest-asyncio to ci 2023-06-08 08:13:28 +02:00
Lasse Blaauwbroek
770be41b6d Bugfix: Attach server to on_disconnect to prevent early closing 2023-06-08 08:11:23 +02:00
Lasse Blaauwbroek
7a8175ed84 Get rid of VoidPromise and (almost) Promise
We only retain RemotePromise for its pipelining capabilities.
2023-06-08 08:02:07 +02:00
Lasse Blaauwbroek
da6a07efd5 No more need for promise joining 2023-06-08 05:34:04 +02:00
Lasse Blaauwbroek
20868d7db0 Get rid of dead code 2023-06-08 04:13:59 +02:00
Lasse Blaauwbroek
4b5c4211f1 Force server methods to be async and client calls to use await 2023-06-08 03:56:57 +02:00
Lasse Blaauwbroek
a69bc72a0b Remove a bunch of unused code 2023-06-08 02:42:04 +02:00
Lasse Blaauwbroek
854d910bee Make tests run faster by reducing timeouts 2023-06-08 02:31:06 +02:00
Lasse Blaauwbroek
af99e388fb Fix and improve a bunch of tests 2023-06-08 02:29:54 +02:00
Lasse Blaauwbroek
b29f18ed64 Cleanup 2023-06-08 02:29:24 +02:00
Lasse Blaauwbroek
97bdeaea12 Disable option to run capnp without asyncio 2023-06-08 02:27:38 +02:00
Lasse Blaauwbroek
6e011cfe78 Get rid of capnp timer functionality.
The asyncio timer should now be used
2023-06-07 20:55:43 +02:00
Lasse Blaauwbroek
8feb377217 Allow reading and writing messages from sockets in async mode 2023-06-07 11:24:43 -07:00
Lasse Blaauwbroek
d53aa24733 Allow async capability implementation methods to return a tuple 2023-06-07 10:51:53 -07:00
Lasse Blaauwbroek
c037342615 Allow capability implementation methods to be async 2023-06-07 10:51:53 -07:00
Lasse Blaauwbroek
d32854eb00
Integrate the KJ event loop into Python's asyncio event loop (#310)
* Integrate the KJ event loop into Python's asyncio event loop

Fix #256

This PR attempts to remove the slow and expensive polling behavior for asyncio
in favor of proper linking of the KJ event loop to the asyncio event loop.

* Don't memcopy buffer

* Improve promise cancellation and prepare for timer implementation

* Add attribution for asyncProvider.cpp

* Implement timeout

* Cleanup

* First round of simplifications

* Add more a_wait functions and a shutdown function

* Fix edge-cases with loop shutdown

* Clean up calculator examples

* Cleanup

* Cleanup

* Reformat

* Fix warnings

* Reformat again

* Compatibility with macos

* Inline the asyncio loop in some places where this is feasible

* Add todo

* Fix

* Remove synchronous wait

* Wrap fd listening callbacks in a class

* Remove poll_forever

* Remove the thread-local/thread-global optimization

This will not matter much soon anyway, and simplifies things

* Share promise code by using fused types

* Improve refcounting of python objects in promises

We replace many instances of PyObject* by Own<PyRefCounter> for more automatic
reference management.

* Code wrapPyFunc in a similar way to wrapPyFuncNoArg

* Refactor capabilityHelper, fix several memory bugs for promises and add __await__

* Improve promise ownership, reduce memory leaks

Promise wrappers now hold a Own<Promise<Own<PyRefCounter>>> object. This might
seem like excessive nesting of objects (which to some degree it is, but with
good reason):
- The outer Own is needed because Cython cannot allocate objects without a
  nullary constructor on the stack (Promise doesn't have a nullary constructor).
  Additionally, I believe it would be difficult or impossible to detect when a
  promise is cancelled/moved if we use a bare Promise.
- Every promise returns a Owned PyRefCounter. PyRefCounter makes sure that a
  reference to the returned object keeps existing until the promise is fulfilled
  or cancelled. Previously, this was attempted using attach, which is redundant
  and makes reasoning about PyINCREF and PyDECREF very difficult.
- Because a promise holds a Own<Promise<...>>, when we perform any kind of
  action on that promise (a_wait, then, ...), we have to explicitly move() the
  ownership around. This will leave the original promise with a NULL-pointer,
  which we can easily detect as a cancelled promise.

Promises now only hold references to their 'parents' when strictly needed. This
should reduce memory pressure.

* Simplify and test the promise joining functionality

* Attach forgotten parent

* Catch exceptions in add_reader and friends

* Further cleanup of memory leaks

* Get rid of a_wait() in examples

* Cancel all fd read operations when the python asyncio loop is closed

* Formatting

* Remove support for capnp < 7000

* Bring asyncProvider.cpp more in line with upstream async-io-unix.c++

It was originally copied from the nodejs implementation, which in turn copied
from async-io-unix.c++. But that copy is pretty old.

* Fix a bug that caused file descriptors to never be closed

* Implement AsyncIoStream based on Python transports and protocols

* Get rid of asyncProvider

All asyncio now goes through _AsyncIoStream

* Formatting

* Add __dict__ to  PyAsyncIoStreamProtocol for python 3.7

* Reintroduce strange ipv4/ipv6 selection code to make ci happy

* Extra pause_reading()

* Work around more python bugs

* Be careful to only close transport when this is still possible

* Move pause_reading() workaround
2023-06-06 11:08:15 -07:00
Jacob Alexander
ed894304a3
Update README.md with stub generation tool. 2023-04-24 09:34:25 -07:00
Lasse Blaauwbroek
6d5ee1c6f2
Use cibuildwheel in ci (#309)
* Use cibuildwheel in ci

`cibuildwheel` is a system that automatically compiles and repairs wheels for
many python versions and architectures at once. This has some advantages vs the
old situation:
- Macosx wheels had inconsistent minimum versions ranging between 10.9 and
  11.0. I'm not sure why this happens, but for some users this means they have
  to build from source on macosx. With cibuildwheel, the build is
  consistent, with 10.9 as the minimum for x86 and 11.0 for arm64.
- Consolidation between the packaging tests and manylinux tests.
- Addition of musllinux targets and additional cross-compilation to ppc64le and
s390x.
- With cibuildwheel, new python versions should be automatically picked up.
- Separation of the sdist build and lint checks. There is not reason to run that
  many times.

All possible build targets succeed, except for ARM64 on Windows. The upstream
capnp build fails. I've disabled it.

The cross-compilation builds on linux are pretty slow. This could potentially be
sped up by separating the builds of manylinux and musllinux, but I'm not sure if
it's worth the extra complexity. (One can also contemplate disabling these
targets.)

Tests for macosx arm64 cannot be run (but also couldn't be run in the previous
system. This should be remedied once apple silicon becomes available on the CI.

I've also added some commented-out code that can automatically take care of
uploading a build to PyPi when a release is created. One might contemplate using this.

* Set CMAKE_OSX_ARCHITECTURES for arm64 and disable universal2
2023-04-03 18:55:17 -07:00
Lasse Blaauwbroek
701cabe61d
Make pycapnp more GIL friendly (#308)
* Mark c++ capnp api with nogil

* Release gil before blocking in capnp during message reading
2023-03-13 09:56:02 -07:00
Jacob Alexander
5061cdd1ee Fix black formatting 2023-03-12 11:33:41 -07:00
Jacob Alexander
d62ba169c9 Update GitHub Actions per deprecation notice
Node.js 12 actions are deprecated. Please update the following actions to use Node.js 16: actions/checkout@v2, actions/setup-python@v1. For more information see: https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/.

Node.js 12 actions are deprecated. Please update the following actions to use Node.js 16: docker/setup-qemu-action@v1. For more information see: https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/.
2023-01-27 14:18:27 -08:00
Jacob Alexander
b791c61f82 Update README.md 2023-01-26 16:55:21 -08:00
Jacob Alexander
33c453eff7 Increment version to v1.3.0 2023-01-26 16:43:40 -08:00
Matt Sachtler
01c4469e08
Add Python 3.11 to Github Actions builds (#306)
* Add Python3.11 to the Github Actions, in the manylinux2014 build as well as the
packaging test.

I added a second image, x86_64, to the manylinux2014 build matrix - the i686
build didn't immediately pass, so I left it out.

* Move the bundled capnproto library forward to 0.10.3.

* Remove the 2010 build entirely, and update the 2014 build to include i686.
2023-01-26 16:28:21 -08:00
André Cruz
1b19b6db13
Prevent race condition in example code (#305)
The example async server code uses timeouts around read() operations.
However, this has a race condition where data can be read, the timeout
fires, and the data is lost.

These timeouts are not really needed in this example code, so I removed
them to prevent people from having strange issues with lost messages
and undefined RPC behavior when using the example code.
2023-01-08 22:29:23 -08:00
Jacob Alexander
9b6ff51f9c Update to v1.2.2 2022-12-01 21:58:30 -08:00