Solved Multi threading in C4D python

hi there,
I managed to get a script working that can select and hide polygons that are outside of the camera frustrum as well as polygons that are behind other polys along a camera path. I’m using the ray collider to detect what poly face has been hit from the camera's position and then selecting this with the polygon selection tag.
I am now trying to use threading in python to split the ray hits calculations between my computer’s threads so that I can speed up the script’s running time. Having some issues at the moment with getting the script to show the selected polys. It works when threading is False, but not sure what I am doing wrong in my setup for when threading is true, any help would be much appreciated

attached is a test file and the python script which runs in the script manager220608_occlusion_frustrum_threaded02.py
A220608_model with path 2.c4d

Hello @anamate,

welcome to the Plugin Café and thank you for reaching out to us. While Python, both our interpreter and the vanilla CPython interpreter, offers parallelism, the module threading is not the parallelism you want. There are two variants of parallelism in most languages:

  1. Parallelism just for the sake of parallelism, this form often slows code down and is only meant to decouple things (a GUI from its logic for example).
  2. Parallelism to improve the speed of execution. The classical example is sorting. You want to sort 100e6 elements and implement a parallelized sorting algorithm which does the job in 10% of the time a sequential algorithm requires.

In Python you can achieve the first form of parallelism with threading or in our Python flavor with c4d.threading (which are entirely different modules, and you must use ours). This form of parallelism is mostly meant to provide something like non-blocking GUIs, handle user inputs in a non-block manner, etc. CPython offers the module multiprocessing for providing the second form of parallelism, but it is sort of a hack, as Python cannot do "make-code-run-faster"-parallelism due to how the GIL works, and multiprocessing is just an elaborate way of running two (or more) interpreters in parallel. The module multiprocessing is not supported by our interpreter, and while you can make it run, it will result in odd behaviors.

Or to quote myself from the docs:

Please note that C4DThread, just as its native Python counterpart threading.Thread, is a mechanism to decouple the execution of code, but not to speed it up. Its intended use is to make computationally complex tasks non-blocking for the main-thread of Cinema 4D and by that ensure the responsiveness of the user interface. Parallelism in the sense of executing multiple parts of a larger task in parallel is currently not offered by the Python SDK. This also applies by extension to the multiprocessing module of Python which offers such functionality but it is not supported by Cinema 4D. Trying to use C4DThread to speed up the parallel execution of such multiple parts task will not only yield no execution speed improvements but will most likely even be slower than a single threaded execution of the same task.

Cheers,
Ferdinand

MAXON SDK Specialist
developers.maxon.net

Hello @anamate,

welcome to the Plugin Café and thank you for reaching out to us. While Python, both our interpreter and the vanilla CPython interpreter, offers parallelism, the module threading is not the parallelism you want. There are two variants of parallelism in most languages:

  1. Parallelism just for the sake of parallelism, this form often slows code down and is only meant to decouple things (a GUI from its logic for example).
  2. Parallelism to improve the speed of execution. The classical example is sorting. You want to sort 100e6 elements and implement a parallelized sorting algorithm which does the job in 10% of the time a sequential algorithm requires.

In Python you can achieve the first form of parallelism with threading or in our Python flavor with c4d.threading (which are entirely different modules, and you must use ours). This form of parallelism is mostly meant to provide something like non-blocking GUIs, handle user inputs in a non-block manner, etc. CPython offers the module multiprocessing for providing the second form of parallelism, but it is sort of a hack, as Python cannot do "make-code-run-faster"-parallelism due to how the GIL works, and multiprocessing is just an elaborate way of running two (or more) interpreters in parallel. The module multiprocessing is not supported by our interpreter, and while you can make it run, it will result in odd behaviors.

Or to quote myself from the docs:

Please note that C4DThread, just as its native Python counterpart threading.Thread, is a mechanism to decouple the execution of code, but not to speed it up. Its intended use is to make computationally complex tasks non-blocking for the main-thread of Cinema 4D and by that ensure the responsiveness of the user interface. Parallelism in the sense of executing multiple parts of a larger task in parallel is currently not offered by the Python SDK. This also applies by extension to the multiprocessing module of Python which offers such functionality but it is not supported by Cinema 4D. Trying to use C4DThread to speed up the parallel execution of such multiple parts task will not only yield no execution speed improvements but will most likely even be slower than a single threaded execution of the same task.

Cheers,
Ferdinand

MAXON SDK Specialist
developers.maxon.net

thanks Ferdinand this cleared things up for me