Enable python 1/2 : C++ interface
This and MR async/paraview!89 add python support in the current code. This MR touches mainly the C++ interface.
Main design:
vtkPytonRunLoop
is modeled after rxqt::runloop
. It combines the application's main thread with the thread of the python asyncio
eventloop. This allows to switch the main thread from an await
on the python side to processing rxcpp events on the C++ side. The main idea is to take advantage of rxcpp::run_loop.set_notify_earlier_wakeup
which sets a function to be executed after an event in pushed to the event loop. The function used in our case is asyncio.get_running_loop.call_soon(delay,F)
which schedules F
with a delay
seconds.
vtkPytonRunLoop::CreateFuture
is essentially a wrapper around asyncio.get_running_loop().create_future()
and allows to create a future on the C++ side , pass it to python side so it can be awaited while still enabling setting from the C++ side.
vtkPythonObservableWrapper
allows to create an asyncio.future
out of an observable. For now we just wrap few methods by hand. It should be updated to have a registration mechanism similar to vtkReflection
or integrated with the PythonWrapper.
Compiled with :
PARAVIEW_USE_PYTHON=ON
VTK_NO_PYTHON_THREADS=NO
VTK_PYTHON_FULL_THREADSAFE=ON
Example taken from Remoting/PythonAsyncCore/Testing/Python/basicInit.py
:
import paraview.modules.vtkRemotingPythonAsyncCore as asyncCore
import paraview.modules.vtkRemotingServerManager as sm
import parat.utils
import asyncio
async def main():
loop.AcquireRunningLoopFromPython()
f = asyncCore.vtkPythonObservableWrapper.GetFuture(
sm.vtkPVApplication.GetInstance(), "CreateBuiltinSession"
)
await f
assert f.done() == True
assert f.result() == 1
Object = asyncCore.vtkObservableExamplePython()
iterator = asyncCore.vtkPythonObservableWrapper.GetIterator(Object, "GetObservable")
async for x in iterator:
print(x)
asyncio.run(main())