15

I have made a python script which prints and logs CPU % and memory usage of a particular process every 2 seconds.

It was working pretty fine. Until I went for a break. (after one hour) When I check again, the python script was paused (or hung). When I pressed [Enter], it started executing again as usual. The script worked fine for 1 hour. Then the log and output were missing for 1hour 30 minutes and not it is working fine again.

What is the cause of the pause.?

How can I prevent it.?

Important Notes:

  • I was working on the machine with RDP (Remote desktop). But the connection was still up and running after an hour.
  • The VM is of OS: Windows Server 2016.
  • Was running the script in Command Prompt.
  • There is no ERROR in "logger.txt".
  • I am monitoring powershell.exe
  • The script does not hang when I monitor any other process than powershell.exe (I tested with python.exe & taskmgr.exe)
  • The powershell process was running for sure. I checked log of the Powershell script and it was running whole the time.

Whole code:

import psutil
import re
import math
import traceback
from time import ctime,sleep,localtime,time
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

import sys

def logg(msg):
    msg_parsed = str(ctime()) + " : " + str(msg)
    with open("logger.txt","a") as ff:
        ff.write("\n" + msg_parsed)

def aprint(msg, process="", counter = "" , pos=0,size=90):
    def sprint(msg):
        if not pos:
            print(msg)
        elif pos==1:
            print("|" + msg[0:size] + " "*(size- (len(msg[0:size]))) + "|" + " "*size+"|")
        elif pos==2:
            print("|"  + " "*size+"|" + msg[0:size] + " "*(size- (len(msg[0:size]))) + "|")


    msg = str(ctime()) + " : " + str(process) + " : " + counter + " : " + str(msg)
    sprint(msg)
    if counter or process:
        f = open("_".join((counter.replace(" ",""), process.replace(" ",""))) +".log", "a")
    else:
        f = open("perf.log", "a")
    f.write(msg+"\n")
    f.close()

    try:
        res = requests.post("https://localhost:8088/services/collector", headers={"Authorization": "Splunk 1b61f1eb-e29f-4d29-9f70-b7f88bcd5b65"}, data='{"time": %d , "index":"main","sourcetype": "FIFA:%s", "event":"%s"}' % (time(),counter,msg), verify = False)
        if "Success" not in res.text:
            sprint("[WARNING]")
            logg(" WARNING : " + res.text)

    except Exception as eee:
        sprint("[ERROR]")
        logg(msg + " ::: coulld not sent :::" + str(eee))
        logg(traceback.print_exc())


def convert_size(size_bytes):
   if size_bytes == 0:
       return "0B"
   size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
   i = int(math.floor(math.log(size_bytes, 1024)))
   p = math.pow(1024, i)
   s = round(size_bytes / p, 2)
   return "%s %s" % (s, size_name[i])


while True: 
    try:
        rrs =0
        cpptime = 0
        leave = False
        matched = None
        for proc in psutil.process_iter():
            if re.match(sys.argv[1].lower(),proc.name().lower()):
                leave = True
                matched = proc.name()
                rrs = max(rrs, proc.memory_info().rss)
                cpptime = max(proc.cpu_percent()/psutil.cpu_count(), cpptime)
        if matched:
            aprint( convert_size(rrs), matched ,"Memory" , pos=1)
            aprint( cpptime , matched ,"CPU", pos=2)

        try:
            sleep(2)

        except:
            break
    except Exception as ee:
        print(" [ERROR] :  " + str(ee))
        logg(ee)
        logg(traceback.print_exc())

Run : python file.py <process name> e.g : python file.py python

Log File:

Mon Jun 18 12:46:30 2018 : powershell.exe : memory : 2.29 GB
Mon Jun 18 12:46:33 2018 : powershell.exe : memory : 2.29 GB
Mon Jun 18 12:46:37 2018 : powershell.exe : memory : 2.29 GB
Mon Jun 18 12:46:40 2018 : powershell.exe : memory : 2.29 GB
Mon Jun 18 12:46:43 2018 : powershell.exe : memory : 2.29 GB
Mon Jun 18 12:46:46 2018 : powershell.exe : memory : 2.3 GB
Mon Jun 18 12:46:49 2018 : powershell.exe : memory : 2.3 GB
Mon Jun 18 12:46:53 2018 : powershell.exe : memory : 2.3 GB
Mon Jun 18 12:46:56 2018 : powershell.exe : memory : 2.3 GB
Mon Jun 18 12:46:59 2018 : powershell.exe : memory : 2.31 GB
Mon Jun 18 14:17:33 2018 : powershell.exe : memory : 3.11 GB
Mon Jun 18 14:17:38 2018 : powershell.exe : memory : 3.11 GB
Mon Jun 18 14:17:41 2018 : powershell.exe : memory : 3.11 GB
Mon Jun 18 14:17:44 2018 : powershell.exe : memory : 3.11 GB
Mon Jun 18 14:17:47 2018 : powershell.exe : memory : 3.11 GB
Mon Jun 18 14:17:50 2018 : powershell.exe : memory : 3.11 GB
Mon Jun 18 14:17:53 2018 : powershell.exe : memory : 3.11 GB

Settings: enter image description here

Jay Joshi
  • 1,402
  • 1
  • 13
  • 32
  • What was the last line the process "hang" after? You could try to add print statements after each individual line, then try to reproduce the problem. (Since reproducing the problem may take hours, please don't expect us to.) – tobias_k Jun 18 '18 at 12:32
  • 9
    My guess would be that this is related to a quirk of running python in powershell. If you click on the powershell window, it pauses execution of your python script until you hit enter. – Chris Mueller Jun 18 '18 at 12:34
  • As I have mentioned in the Post now, I was executing the script in Command prompt. – Jay Joshi Jun 18 '18 at 12:39
  • I feel the culprit can be `psutil` module.? – Jay Joshi Jun 18 '18 at 12:42
  • @ChrisMueller, Actually, I was monitoring powershell.exe. Is the issue is in the PowerShell process itself? – Jay Joshi Jun 18 '18 at 13:20
  • 1
    Possible Reason : https://stackoverflow.com/questions/30418886/how-and-why-does-quickedit-mode-in-command-prompt-freeze-applications – Jay Joshi Jun 26 '18 at 10:03
  • 1
    Just for the record: https://bugs.python.org/issue26744 – Bruce Oct 27 '21 at 22:17
  • 1
    Happens to me on Anaconda Prompt on Windows 10 also. – user8491363 Jun 12 '22 at 07:41
  • I face the same issue even today, you found a proper solution to this? Execution just hangs. System is awake. Press enter and it continues executing. – PythoLove Jul 05 '22 at 12:30

1 Answers1

17

I used the following minimal example to reproduce your problem:

import time
while True:
    print(time.ctime())
    time.sleep(2)

It just prints the time every two seconds. I ran this script from CMD using the following command:

python test.py

The only thing that stopped the script, was when I (left) clicked into into the console and entered the QuickEdit mode to highlight and copy text from it. So maybe you just accidentally clicked into the CMD, when you went to your break. You can prevent that behaviour by just redirecting the output. Use this in CMD:

python test.py > out.log 2> err.log

or this in PowerShell:

Start-Process python -ArgumentList "test.py" -RedirectStandardOutput out.log -RedirectStandardError err.log -NoNewWindow -Wait

to redirect both, the output and the error stream. Calling python like that, entering the QuickEdit mode will not pause the execution of your script anymore. In both consoles, you can still terminate the script by pressing CTRL+C at any time (so it is not a background process, which would be a solution, too).


You can also disable the QuickEdit mode, which is enabled by default:

  1. Right-click the title bar of your console window.

  2. Choose Properties.

  3. In the Options tab, go to Edit Options and uncheck the QuickEdit Mode option.

stackprotector
  • 10,498
  • 4
  • 35
  • 64