When I debug a multi-threaded Python program using vscode, I found all threads were suspended when a breakpoint is hitted. Is there any way to choose to suspend only one thread or all like what Pycharm does?
Related
Using pybind11, I'd like to embed multiple interpreters: I want to launch multiple threads, and in each thread, start an interpreter, eval_file, and then stop the interpreter and shut down the thread.
Basically, this is a thread function:
pybind11::scoped_interpreter guard{};
pybind11::eval_file(python_filename);
Compilation is fine, but at runtime I get:
terminate called after throwing an instance of 'std::runtime_error'
what(): The interpreter is already running
I guess this is a no-no. But why? How would it even know?
I am using Python Multiprocessing for a project and sometimes the process freezes and apparently the reason why it is happening is this process I find running ps aux:
python -c from multiprocessing.semaphore_tracker import main;main(39)
Some more info:
If I kill the process everything runs fine
This problem is not frequent, meaning there could be days running everything fine without it happenning
I am using PyCharm
I am runing this Python code in a server using PyCharm remote interpreter and sometime using SSH
Questions:
What is happening that this process is appearing?
Why isn't it finishing by itself?
What does it do that freezes other processes?
How to avoid this situation?
According to the documentation:
On Unix using the spawn or forkserver start methods will also start a semaphore tracker process which tracks the unlinked named semaphores created by processes of the program.
Why one would want to use the spawn start method escapes me. It is a (very clever) bodge necessary on ms-windows because that OS doesn't have the fork system call.
So I suspect that Pycharm imposes the use of the forkserver start method because it uses multiple threads internally, and the standard UNIX fork startmethod doesn't deal well with multithreaded programs.
Try running your project from a shell. On UNIX-like operating systems that should default to the fork start method that does not require the semaphore tracker process.
I want to remote control 'my_program' using os.
import os
os.system(my_program)
While in debug mode in VS-Code i can start the 'my_program', but as soon as it opens, i've no available thread in VS-Code to work with. Or atleast that's what i'm interpreting by the message 'Unable to find thread for evaluation.'. I can not execute any commands in the debug console anymore, for instance 3+3, which should output 6.
As soon as i manually kill 'my_program' by simply closing it in the GUI, i can continue debugging.
In Short:
What i want: Open, use and Close 'my_program' with VSCode using os.system
What i get: Program opens, but VS debugging is somewhat offline, since it is 'Unable to find thread for evaluation.' and i can not continue debugging/closing the program via os.system
os.system("TASKKILL /F /IM my_program.exe")
EDIT: i can reproduce the same behaviour (freezed debugging console) using
subprocess.call(my_program)
I had the same error message. The introspection just works when the debugger is stopped at a breakpoint. I've put a breakpoint in the code, stopped in it and everything works fine.
It works with
subprocess.Popen(my_program)
subprocess.call and os.system block the terminal as long as the process being called is finished, thus 'freezing' the thread and debug window. subprocess.popen on the other hand is an asynchronous call which let's you interact with the terminal while the called process keeps running in the background
I'm fairly new to Python and have been using Wing IDE to play around with the features. One of the things that I could find while looking around was how to force terminate the Python shell when executing a command that won't terminate any time soon. An example would be:
import math
math.factorial(1000000)
I know in Visual Studio C++, the command is Ctrl+C, but what exactly is the Python equivalent?
The method used to terminate execution varies between shells. For Wing IDE you use the Restart Shell item on the Options menu.
This depends on your shell. For most shells, it is ctrl-C, or killing the process.
There is no way to do so from within python (unless you are spawning threads or processes) because the thread in question is stuck.
When my script sleeps for 50sec my IDE locks up which is very annoying. I cant switch tabs, look through my source, type code, etc. It happens in pylde and pyscripter, i havent tried other IDEs. What can i do to fix this? i'm actually doing
for i in range(0, timeInSeconds): time.sleep(1)
hoping the IDE will update once per second but it doesnt look that way. What can i do to fix this?
I'm assuming you are running your code from within the IDE?
Your IDE is probably blocking while running your code. Look for a setting of some sort which might control that behaviour, otherwise I think your only choice would be to change IDE. (Or, run your code from outside the IDE)
Can you configure to run your script externally? I don't know about the specific IDEs, but I would try to spawn a different process for the debugged script and not run them under the IDE. If that doesn't help, then it is a problem of the IDEs.
The problem is your IDE not python. I don't use sleep that often, I've just tried it on the Eric IDE and you can use your IDE while your code is running, and sleeping. If can't set your IDE to do so and you need it then consider to change IDE or to run your code from console.
Personally, I think you should never ever ever execute code in the same loop as your IDE. Since most IDEs run a GUI mainloop, blocking this will cause complete freeze of the user interface. It is just asking for trouble, and I would take out bug reports against both those IDEs.
I suspect the problem the IDE is sitting in a loop waiting for the script to finish.
That in itself is not a problem, provided any user generated messages are still processed while the IDE is in this loop.
But what I suspect is going wrong in this case is the IDE is just running the loop without processing and messages and hence the user interface appears to be locked.
The IDE would need to be changed to either process GUI messages while in the loop or alternatively it needs to create a thread to run the the script. The thread would then run in the background and the GUI would remain responsive.
For example the Zeus for Windows IDE uses the background thread approach and it does not have this problem.