atmtools / arts

The Atmospheric Radiative Transfer Simulator
https://www.radiativetransfer.org/
Other
62 stars 28 forks source link

Free ARTS from the GIL #809

Closed olemke closed 1 week ago

olemke commented 2 weeks ago

The Python GIL (Global Interpreter Lock) ensures that only one thread at a time can execute Python code. It is also engaged when ARTS C++ functions are called. This posed an issue when using multithreading in Python, especially in even-driven frameworks such as GUI frameworks like Qt. In these circumstances it is beneficial to run ARTS calculations in a separate thread and not on the main thread which handles the event loop. However, with the GIL engaged, the main thread will be stuck until the ARTS calculation has finished. To avoid this, this PR releases the GIL when calling into C++ code. For those cases when the ARTS code might call back into Python code (e.g. our operators), the GIL is temporarily reengaged to ensure some level of thread safety. If the user decides to use the same ARTS workspace from different threads at the same time, it is up to them to use a mutex or other means of synchronization to establish thread safety. Otherwise, things can and will go wrong!