My Mac Book Pro switch between charge and not charge when running a long program

When I am running a very long program (e.g. A long python script) and when I plug in the charger, my laptop will switch between charging and not charging very frequently. However, if I stop that script, my laptop will charge normally when it is plugged in.

Does anyone know what might be causing this and how to fix it?


There's actually nothing you need to fix - it is intended behavior.

Consider that your laptop at idle is consuming (fictious numbers) 10W of power. You plugin a 61W power adapter, and it starts charging the battery at a rate up to 51W. I.e. this means that the power from the power adapter is used to power the computer primarily, and secondly to charge the battery.

Now you run a very power hungry program on your computer, and the computer starts to use 65W of power. Now all the power from the power adapter is used to power the computer, and the battery is also discharged slowly. This means that the status on the display will change from "charging" to "not charging".

Your Python script most probably does different things at different times - some that take a lot of power and some that take less power. As a result, you'll see that some times the battery is being charged, and at other times it is not being chargd.