How can I tell if my laptop is being charged in Python? - python

I have been working on some python code that does heavy calculations, and it's finally finished. I am now running the code as much as possible, but because it uses a constant +- 70% processor and 1,5Gb ram it uses a lot of power. For this reason i only run the script when i am charging my laptop, but i often forget.
What i want to do to solve this problem is making it automatic, of course. My plan is to always have the script running, but when my laptop is not charging it will be idle.
The main problem with this is that i have to detect that my laptop is being charged. I have looked around for information on this, but i can't find any that suits my problem. I have seen people use the ctypes library, but i can't seem to find a function that tells me if my laptop is being charged.
I am looking for a python function that tells me if my laptop is being charged.
edit: My operating system is windows 10.

Related

My python is using less of my system resources than it used to when training my ML models

I am working on a project with some ML models. I worked on it during the summer, and have returned to it recently. For some reason, it is a lot slower to train and test now than it was then--I think it is a problem with python not using all of my system resources all of a sudden.
I am working on a project where I am building, training, and testing some ML models. I am using the sktime package in a python3.7 conda environment with jupyter notebook to do so.
I first started working on this project in the summer, and when I was building the models, I timed how long the training process took. I am resuming the project now, and I tried training the exact same model with the exact same data again, and it took around 6 hours this time compared to 76 minutes when I trained it during the summer. I have noticed that running inference on the test set also takes longer.
I am running on an 10-core M1 Max with 64 Gigs of RAM. I can tell that my computer is barely breaking a sweat right now, and with activity monitor it says that python is using 99.9% CPU, but it says overall the user is only using around 11.60% of the CPU. I remember my computer working a little harder when I was working on this project during the summer (the fan would actually turn on and the computer would get hot, but that is not happening now), so I have a feeling that the problem here is that my environment is not using the full system resources accessible to them. I have checked and my RAM limit on jupyter is enough, so that is not the issue. I am very confused what could have changed in this environment between the summer and now that has caused this problem. Any help would be much appreciated.

Python: Does Having Old Laptop Impact Runtime on Looping Programs?

I'm running a self implemented algorithm on my personal laptop using python 3.8. It's taking over 5 minutes to run while other people seem to be able to run it in 2 min or under.
I have an old laptop. Does this impact my runtime significantly?
It can't be told if it will impact significantly or not without seeing your algorithm and your hardware specs.
But having old hardware will surely impact your program runtime.
You can optimize your code for your hardware specifically to improve runtime.
Although python is very flexible and can run well in older devices.

How to "own" a processor?

I have a somewhat different problem to solve with threads in Python.
I have a Python application that talks to very high-speed hardware through a polling interface. I would like the Python process to "own" one of the CPUs, that is, I don't want the Python application to share its CPU with any other process.
What I am currently seeing is that my application gets put "on hold" for tens of milliseconds at a time while it is servicing hardware that executes commands in tens of microseconds, thus the hardware goes idle while my Python application is put on hold.
I am running under Windows 10. Is there a system call of some kind that lets me own the CPU?

Why does Raspberry Pi need an OS to work?

I am just new to world of electronics. I was just looking about Raspberry Pi as I want to work on it, but a thing that I found that everyone is installing an OS first on the Raspberry Pi. I have worked upon PICs(16f) and Arduino board and it doesn't need that I just write a code and make burn on the board Can I do this with the Raspberry Pi?
Can't we just use an IDE to program the Raspberry Pi in python rather than installing an OS? Please explain if I conclude something wrong about the Raspberry Pi.
Long story short, the Pi is too complex hardware-wise to operate without an OS (for the vast majority of tasks, anyway).
An operating system is essentially an environment for your program to work in. It provides standardized means to use and manage hardware, interrupts, storage (incl. filesystems), I/O etc. What is more inportant, it does all the non task-specific heavy lifting in those tasks. So, you yourself only have to implement task-specific logic.
Without an operating system, you'll have to include all that functionality (or rather, the part of it that your task-specifc logic needs) into your program (that is called a "bare bones" programming environment). If the hardware is simple and unified enough (e.g. a specific model of a microcontroller), that it easy enough. But if the task of operating the hardware is complex enough and/or you need to support many different hardware configurations, it simply becomes too taxing to implement all that by hand each time.
For example, you said you'd like to write your program in Python.
A Python program doesn't exist in vacuum. It's run by a Python interpreter -- that needs to already be installed on the system so that you can feed your program to it. Likewise, the interpreter itself is written for a specific environment. E.g. CPython uses the C standard library. That library in turn may delegate work to whatever environment it is written to work in -- e.g. system calls of a specific OS. Finally, the OS, run by the system's CPU, interacts with peripheral devices in whatever ways the specific hardware environment is designed to (I/O ports, memory-mapped I/O, various standardized I/O protocols (like SATA) which typically consist of reading and writing hardware registers in devices and handling interrupts from them and the CPU itself; large data transfers are nowadays usually done with DMA, a session of which the OS logic still needs to set up.)
So if you're going to work without an OS, you'll need to implement any of those layers yourself first that would work in a Pi bare bones environment -- all just to run a simple Python program. You can take a look at Raspberry Pi Bare Bones - OSDev Wiki to get an idea of what that experience is like.
Raspberry pi basically is a computer. It's not a microcontroller so it's different with arduino. To do the microcontroller stuff with raspberry-pi all you need is wiringpi module you can download it freely. But raspberry pi is bigger than microcontroller so, it is wise that it used to controlling multiple microcontroller.
Raspberry pi is more complex, with a microprocessor, memory and GPU. Its like a full fledged computer, basically it needs a OS to managed everything unlike micro controllers like Arduino.

Portable way to check if virtualized OS

Is there any portable way or library to check if Python script is running on a virtualized OS and also which virtualization platform it's running on.
This Detect virtualized OS from an application? questions discusses a c version.
I think you call linux command virt-what in python.
The descriptio of virt-what is here: http://people.redhat.com/~rjones/virt-what/
To my knowledge, there is no nice, portable way to figure this out from Python. Most of the time, the way people try to figure out if they're being virtualized or not is looking for clues left by the VM -- either some instruction isn't quite right, or some behavior is off.
However, all might not be lost if you're willing to go off box. When you're in a VM, you will almost never have perfect native performance. Thus, if you make enough measurements against a server you trust, then it might be possible to detect if you're in a VM. This is especially the case if you're running on a machine with multiple machines. Check your own time, how much time you're getting scheduled, and how much wall time has past (based on an external measurement because you can't trust the local machine). You'll probably have better luck if you can watch how much time has passed on the local machine rather than just inside one process.

Categories