Commit graph

37 commits

Author SHA1 Message Date
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
Lasse Blaauwbroek
84d0f365ad Fix formatting 2023-06-08 08:18:50 +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
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
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
6e7fffd7de
Applying black formatting
- Fixing flake8 configuration to agree with black
- Adding black validation check to github actions
2021-10-01 11:12:21 -07:00
John Vandenberg
8c15feb4eb Remove many lint warnings
Mostly whitespace changes, with max-line-length set to 120
and double-quotes used, adopting black settings.

Related to https://github.com/capnproto/pycapnp/issues/128
2021-05-03 17:02:30 +08:00
Andrey Cizov
d94d60db8b
get rid of all warnings during tests 2019-12-26 22:25:56 -08:00
Jacob Alexander
fc75e4083d
Fixing remaining flake8 lint warnings 2019-12-11 22:44:44 -08:00
Jacob Alexander
b3021e4f6b
Fixing flake8 warnings and errors
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --exclude benchmark

Excluding the benchmark directory (due to protobuf generated files)
Also removing some Python2 specific code
2019-09-27 00:15:13 -07:00
Jason Paryani
a4990f0868 Handle AnyPointers better as arguments to RPC functions 2015-06-08 14:59:21 -07:00
Jason Paryani
6f1ce8bcc5 Add support for using keyword arguments with a named struct in an RPC 2015-06-08 13:39:10 -07:00
Jason Paryani
8f78a7c80b Add null capability test 2015-06-03 14:35:54 -07:00
Jason Paryani
b9c9c6bc5c Fix for upstream changes in kj::Exception
* Change from using Nature/Durability to Type
* Prefer to raise KjException directly instead of Value/RuntimError.
* Will still raise AttributeError appropriately.
2014-11-30 14:27:10 -08:00
Jason Paryani
03faa0e18b Get inheritance working for simple version capabilities 2014-04-18 18:18:54 -07:00
Jason Paryani
56d68325ff Add check that method exists in Capability.__getattr__.
This makes it play nice with hasattr
2014-04-17 20:53:52 -07:00
Jason Paryani
430890f8d6 Add args checking to Void and RemotePromises then and check for double
send in Requests.
2014-04-13 18:35:53 -07:00
Jason Paryani
cfb60cddf0 Add arguments checking to then callback 2014-04-13 18:26:01 -07:00
Jason Paryani
c2b9d14268 Fix up cancel and timer a bit 2014-04-13 18:15:36 -07:00
Jason Paryani
f58f5d4846 Add timer functionality 2014-04-10 19:08:24 -07:00
Jason Paryani
f89af26fc3 Fix tests for change in exception type 2014-01-14 09:44:21 -08:00
Jason Paryani
5811213321 Add test for returning tuple in server 2013-12-11 01:17:23 -08:00
Jason Paryani
3f7c3ae023 Change server instantion to be inherited 2013-12-09 17:13:43 -08:00
Jason Paryani
90bf090db1 Update for API changes and add tail_call functionality 2013-12-05 00:07:51 -08:00
Jason Paryani
888b906781 Remove all public references to EventLoop to match changes to async API 2013-12-02 17:38:32 -08:00
Jason Paryani
8e1895dd8f Simplify server interface 2013-11-14 23:06:14 -08:00
Jason Paryani
2e4efd040b Allow client requests to use positional args 2013-11-14 20:59:21 -08:00
Jason Paryani
c1952a62ae Fix exception propogation in Promises 2013-11-13 20:54:57 -08:00
Jason Paryani
5251cb5575 Add upcast/cast_as to DynamicCapability. Also changed EventLoop.
wait_remote -> wait
2013-11-12 20:28:23 -08:00
Jason Paryani
d7abfae1ed Cleanup exception handling 2013-10-20 18:46:41 -07:00
Jason Paryani
5e00534842 Fixup exception handling for capabilities 2013-10-20 17:24:59 -07:00
Jason Paryani
a9ad0e6b85 Make test simpler 2013-10-19 22:40:55 -07:00
Jason Paryani
c3354e9069 Add ability to pipeline rpc requests 2013-10-19 22:38:10 -07:00
Jason Paryani
96bfc495d8 Pipelining almost completely wrapped
Waiting on some upstream changes in C++ libcapnp before I can finish
2013-10-17 22:42:14 -07:00
Jason Paryani
7f7b28f328 Fix memleak and simplify dynamic client api 2013-10-17 19:15:40 -07:00
Jason Paryani
a5d0abb49f First working version of capability interfaces 2013-10-15 22:36:14 -07:00