Replies: 1 comment
-
|
I like the with_handle approach. I think that one also doesn't need to be upstreamed to work, right? I think caster struct should embed the base caster, because sometimes the caster stores necessary additional objects I believe. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This touches on #5684
I'm working on tensorstore (http://github.com/google/tensorstore) free threading (google/tensorstore#218) and looking at possible
py::scoped_critical_sectionuse.I'd like a way to easily tag a
cls.def(...)or other method definition as requiring a critical section. The only current option is to add a wrapper type which adds a mutex to the existing C++ type.This is somewhat important as any mutable access in free-threaded mode likely requires a critical section. That is, basically any type which allows internal/mutable updates via
cls.def_property(getter, setter, ...)should use some sort of synchronization. This synchronization is difficult to achieve using thepy::call_guard<T>mechanism, since the guard_typeTmust be default constructible. This works fine for using a GIL lock, but unfortunately for free threading, there isn't a way to provide thepy::handletopy::call_guard.Tensorstore has quite a few such types where adding such a wrapper type would be a burden; in lieu of this we'd rather rely on the built-in python mutex (https://peps.python.org/pep-0703/#reference-counting) for most simple types.
It appears that the other path to handle this is to use
py::handle selfas the self parameter tocls.def...calls, do the locking,then add the
py::cast<T>call in each location. Of course, the difficulty there is that avoids much of the checking on the invocation side.Something which used to be this:
Becomes much more ungainly:
Wouldn't this be nicer:
The current init, precall, postcall methods of
process_attributeare insufficient, unlessIn order for the with_critical_section to work, we'd probably need to add roughly the following:
file attr.h:
file cast.h:
There are other features which could, potentially, be useful. For example, we could have a wrapper type which allows
passing both the handle and the result of cast_op to the caller.
Which we could probably achieve via something like this:
Ping @rwgk to get your thoughts here. Also @jbms
Beta Was this translation helpful? Give feedback.
All reactions