Python Process (Activity Monitor) Doesn't Stop - python

I recently started using Python. I realized that even the simplest program starts a Python process that never ends and causes my computer to overheat if I don't manually kill that process.
I've even seen multiple Python processes running at the same time after running a few easy Python programs (Hello, World!) in a row.
I updated to the latest Python version (Python 3.9.3), installed all the Certificates, and tested a few different programs to see if it happens every timeā€”it does. I am using VSC as the IDE but the same situation happens even if I use IDLE.
My questions are: Is this an interpreter issue or a Mac issue? Can it be fixed?
Thank you very much.

Related

Does each IDE needs its own Python?

I have PyCharm, Visual Studio Code and Jupyter Lab installed on my PC, and the PC has Python 3.11 installed?
Would someone please educate me on this topic?
If I want run 3 Python programs simultaneously (each program uses its own IDE). Would each IDE run faster if each IDE has its own Python program? Right now all IDEs are using the same Python 3.11 and things are working fine. Just for my own knowledge, I am just curious of this topic. Thank you in advance for educating me!
You have to discriminate the two following concepts: program and process.
Program
A program is a list of instructions. It's generally written on a file.
It can be a binary text but also an ASCII text.
A program does nothing. It is basically a static text.
Process
A process is the execution of a program by your operating system.
A process has a dedicated memory and execution time. Its role is to "play" your code, instruction by instruction.
In your example, python.exe is a program.
When you execute it, your operating system will create a dedicated process.
So, when you run several IDEs, you simply run several process of your same program.
Thus, there is absolutely no loss in terms of performance.

Python Debugger Freezing After 5 Lines of Code

What to do when the python debugger seems to get stuck after running the first few (five) lines of code?
I have been using Python for quite a while, but starting yesterday, I have not been able to use the debugger. Whenever i try to run the debugger, it just displays the first 5 lines of the script and then stops.
If I then try to exit out of the debugger, it just displayer --KeyboardInterrupt-- but it does not terminate the process.
I am running Python 3.9 with Spyder, and have tried fixing it by restarting, resetting to factory setting, installing into a new environment, as well as a total uninstall and clear, and then installing from scratch, but nothing seems to work.
Even when i try running really basic scripts (like the images below) the result is the same
Running basic scripts normally (F5) works fine
But debugging just gets stuck
And terminating the debugger process is not possible

Python programs slow to start in VS Code terminal

When I run any program in the VS Code terminal, the program takes about 20-30 seconds before it starts running. After it starts, it runs at relatively normal speeds.
This is new behavior: code I was running last school year had much faster startup times. I am using no new extensions I am aware of that would be slowing this down- I have tried disabling extensions I don't need, but to no avail. None of the listed startup times appear to be the problem (Python extension's time is 282 ms, and that's the longest listed startup time).
I've tried uninstalling and reinstalling VS Code, and also tried installing Python 3.10.0 (I am using 3.9.0 by default).
Here is a sample of code that shouldn't take much time to start up, but does:
var = 1
while True:
print(var)
var += 1
This slow-start behavior is the same across all Python programs I've been running, infinite loop or no.
VS Code info:
Version: 1.61.2 (user setup)
Commit: 6cba118ac49a1b88332f312a8f67186f7f3c1643
Date: 2021-10-19T14:57:20.575Z
Electron: 13.5.1
Chrome: 91.0.4472.164
Node.js: 14.16.0
V8: 9.1.269.39-electron.0
OS: Windows_NT x64 10.0.19043
FYI, I am a programming teacher, with no educational background in programming. I promise I'll do my best to answer your questions, but I may need some handholding to get there. Thank you for any help.
Edit: I've been in the habit of killing the terminal when I make changes to a program, then saving my program, then running it. I kill the terminal a lot. It turns out that if I only save and rerun my program (without killing the terminal), the first run starts slow, but then subsequent runs start just fine. This leads me to believe that the problem isn't with the program itself, but with my terminal starting slowly.
Going from your comments, I'm pretty sure the problem is not python or VS Code in your system, but PowerShell taking a much longer time to start.
I would advise you to switch to CMD, which takes much less time to start. Go to setting.json and add the line
"terminal.integrated.defaultProfile.windows": "Command Prompt"
Alternatively, you can click the + button in the integrated terminal, select Default Profile and select Command Prompt when the prompt appears on the top of the screen, as shown in the documentation of VS Code
If you want to fix PowerShell being slow itself, there are a lot of answers available online. You seem to be using an older version of PowerShell. You can update it to PowerShell Core 6, but I don't think it would be necessary. Changing the default profile to CMD should be enough to solve your issue.

Use Intel Pin to instrument Python scripts

I need to instrument a Python script using Intel Pin for ChampSim simulator.
The problem is, whenever I run the tool, the script does not seem to run as nothing is printed. Moreover, no matter how long/complex the script is, the trace always ends up with a size of 62M (this is also the case when I simply instrument the interpreter without any script).
I tried running the solution from this post, but it didn't work either. For reference, I am running the following command:
../../../pin -t obj-intel64/champsim_tracer.so -- ./python_script.py
Is it even possible to instrument a Python script? If yes, please detail the steps. Thanks!

psutil - suspending process

I'm writing a script in Python for Linux that should suspend process when given conditions are met and later resume it. To do so I'm using psutil (http://code.google.com/p/psutil).
The problem I encountered is rather strange. When I use python command line, everything works fine - process is suspended and later resumed. However, when I use the same function in a script file and then I try to run it, it throws exception:
AttributeError: 'Process' object has no attribute 'suspend'
Snippet of code:
p = psutil.Process(19931)
p.suspend()
print 'suspended'
time.sleep(10)
p.resume()
print 'resumed'
Of course 19931 is typed PID of the process I've started in background and want it to be suspended.
Does anyone have some ideas or hints? Psutil is the newest version installed from Google code.
I know its an old entry but for the ones who encountered this problem recently,
this code works fine for me (Python 3.7.3) :
psutil.Process(pid=19931).suspend()
And similarly
psutil.Process(pid=19931).resume()
But you have to be sure that referred process is still alive.
The first thing I would suspect when Python behaves differently under the interactive shell and executing a script would be running different versions of Python. This can easily happen when you point to an interpreter in your script file with something like:
#!/usr/bin/python
This is a long shot since, according to the psutil website, it's supposed to work well with Python versions from 2.4 to 3.3. Also, your snippet works fine on my machine (kernel 2.6.32, Python 2.6.6).

Categories

Resources