0

I'm writing an application working with plugins. There are two types of plugins: Engine and Model. Engine objects have an update() method that call the Model.velocity() method.

For performance reasons these methods are allowed to be written in C. This means that sometimes they will be written in Python and sometimes written in C.

The problem is that this forces to do an expensive Python function call of Model.velocity() in Engine.update() (and also reacquiring the GIL). I thought about adding something like Model.get_velocity_c_func() to the API, that would allow Model implementations to return a pointer to the C version of their velocity() method if available, making possible for Engine to do a faster C function call.

What data type should I use to pass the function pointer ? And is this a good design at all, maybe there is an easier way ?

1 Answer 1

1

The CObject (PyCOBject) data type exists for this purpose. It holds a void*, but you can store any data you wish. You do have to be careful not to pass the wrong CObject to the wrong functions, as some other library's CObjects will look just like your own.

If you want more type security, you could easily roll your own PyType for this; all it has to do, after all, is contain a pointer of the right type.

Sign up to request clarification or add additional context in comments.

3 Comments

How can I pass this from my Model to the Engine ? all I have from the Engine point of view is a Python instance of the currently used Model, I don't know how to "call" the C code (which may not even exist) in the Model from the Engine.
I realize my comment does not make sense... I mean, how can I pass a PyCObject from C to Python ?
Never mind I understand now, I just do PyCObject_FromVoidPtr / PyCObject_AsVoidPtr on both sides, thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.