ROOT.gApplication.Run() and threading

Hi,

ROOT.TThread.Initialize();should be in the main thread before any thread is spawned.
and then you indeed need:rt = ROOT.Thread()inside the code executed by each thread before any other ROOT code is executed within that thread.

Cheers,
Philippe.

Hi,

code run from Python threads is thread-safe b/c of the GIL.

Cheers,
Wim

[quote=“pcanal”]Hi,

ROOT.TThread.Initialize();should be in the main thread before any thread is spawned.
and then you indeed need:rt = ROOT.Thread()inside the code executed by each thread before any other ROOT code is executed within that thread.
[/quote]

Unfortunately it did not help. However, I fixed the high CPU problem, which I describe in another post.

[quote=“wlav”]Hi,

that one I do understand and is the motivation for the refactoring. It’s the problem as seen in GaudiHIve: after releasing the GIL, when the C++ code calls back into Python again (after having started from a python call that release the GIL), the GIL needs to be re-acquired. That has to be done on the C++ side (in TPython::Exec).

Okay, another option then. Instead of gApplication.Run(), use:while 1: try: ROOT.gSystem.ProcessEvents() time.sleep( 0.01 ) except Exception: # in case gSystem gets destroyed early on exit passAnd make sure that the GUI thread is still switched off as before.

What this does, is the work of the GUI thread (from which the above code is lifted), but now on the main thread. The “while 1” will prevent exit until the interpreter exits. Since it is in python, it will play nice with other python threads.
[/quote]

So, it works OK. My problem was that I was not importing time, which was interpreted as an exception and mishandled, without any wait. Thus the high CPU load.