running a thread-based python script from PythonQt
Dynamic Python binding for Qt Applications
Brought to you by:
florianlink,
marcusbarann
Hi
I've used r502 version of PythonQt with thread support, I wanted to run a special simple script below but I'm getting crashed when I want to run it from PythonQt framework:
I've set
PythonQt::setEnableThreadSupport(true)
it gives me the errors like below (in some somehow same situtuation with a little twerk):
1:
2:
3:
is there something else I should do? so I can get a python thread running in my script to work fine?
Qt does not allow to create widgets outside the GUI thread, this a Qt limitation, not a Python limitation.
Thank you Florian for your attention and reply
I've managed somehow to make a debugger with PythonQt and bdb library of python.
the problem is that bdb or other python debuggers are thread-based and if I use PythonQt_QtAll GUI elements it will be trapped in the thread of python I think and will crash out to desktop when stepping over script lines.
do you have any suggestion for a way that I can manage it to work or any other efforts that I should follow maybe?
Regards
I think you have to look into remote debugging, e.g. like in PyDev /Eclipse or PyCharm.
Running a debugger in the same process with Qt Gui is not possible, because the main thread will be blocked, so your Gui can't update. You need a second process which shows the Debugger and has a UI, typically that application is the text editor as well, so that you can set breakpoints and show where the cursor is located. Maybe using an existing remote debugger would be a simple solution for you (but less integrated and customized, of course).
Thank you so much
right now I don't have any problem in debugging a GUI-less script like even working with models and other libraries of Wrapped Qt classes and it works fleuntly. I developed a UI with text editor, highighting syntax, breakpoints and any other concepts required by a simple debugger like variables table etc. but when it comes to the wrapped GUI classes it will shoot a fatal error to screen when I'm stepping over the commands related to the GUI.
so do you mean I can also debug GUI-based script lines with a remote debugger?
is there some example for the way you suggested to implement a multi-process application to attach a debugger to the script from another process? right now I just have 2 threads in my debugger component , one for GUI and one for debugging with bdb. but I still recieve the error.
Last edit: Mortie 2019-01-21
I've tricked this task by using queued signal and slots mechanism for GUI components that are going to be debugged. so the wrapped methods will just rise a signal and it'll catch it's appropriate method by pre-defined connections with Qt::AutoConnect so GUI components will decide how to connect to those relative slots based on the method they're running. (debug or just runeval)
I know this is not a professional method for doing these stuff but it just satisfies my needs until now.
So, in 2019 closing to the end, what is the status of threading module support in PythonQt? I see the
PythonQt::setEnableThreadSupport(true)
method in the docs, and I call it when my app starts. Nevertheless, when I start threads in embedded interpreter, they are not working as expected. NOTE: I am not asking for help - just a short answer if threading is supported in last version of PythonQt (as suggested by the existence of the above-mentioned method) or not.P.S. Below is the code I use in python. If started from PythonQt embedded interpreter, only one line
running...
is shown.Last edit: Max 2019-09-02
Threading in PythonQt in fully functional and working.
There is one thing that you have to keep in mind:
By default, the main thread of your application will hold the GIL, so other threads can only get a time slice when the main thread is running in a Python loop.
So what you have to do is to release the GIL after you started up PythonQt and retake it whenever you want to talk to Python from C++ from whatever thread.
You release the GIL after the initial PythonQt initialization by:
There is a macro for taking and releasing the GIL, called PYTHONQT_GIL_SCOPE, place this at all code that talks to PythonQt or Python itself.
Last edit: Florian Link 2019-09-02
Thanks for your help. The problem was that I am using PythonQtScriptingConsole, and after releasing the GIL, the app seg-faulted whenever I just typed into the console (without even pressing Enter). But if I do not interact with the console but just evaluate a script from my c++ app, then python threads run normally without blocking my c++ app. (Still, it's a bit surprising that seg-fault happens when I type a single letter in the console -- is python interpreter invoked without even pressing Enter?).
Last edit: Max 2019-09-06
yes, for autocompletion. I have not yet adapted toconsole, becausewe have our own console in our product. It just needs additional GIL scope locks to work with threading.