Python CPU使用率下降到0%,在脚本执行过程中击键后恢复 [英] Python CPU usage drops to 0%, resumes after keystroke during script execution

查看:298
本文介绍了Python CPU使用率下降到0%,在脚本执行过程中击键后恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与此处发布的问题几乎相同:

my issue is nearly identical to the issue posted here:

Python睡眠直到击键

该线程多年来一直处于非活动状态,如果存在用于重新打开"该问题的不同协议,请告知-我正在同时发布此问题,如果我应该这样做,我会提前道歉不一样.

That thread has been inactive for years and if there's a different protocol for "re-opening" the issue please advise - I'm posting this question in the mean time, and I apologize ahead of time if I should be doing this differently.

我无法发布代码,但是我可以分享一些细节-我正在执行一个脚本,其中包含许多迭代生成的打印语句,以跟踪脚本执行所花费的几个小时的进度.在任务管理器中监视我的CPU使用情况时,我可以看到使用率定期下降到0%,并且仅当我在运行脚本的实际命令提示符中输入任何种类的击键时才恢复使用.

I can't post the code, but here are some details I can share - I'm executing a script that contains many iteratively generated print statements to track progress over the several hours the script takes to execute. While monitoring my CPU usage in Task Manager, I can see that periodically the usage drops to 0% and only resumes when I enter any kind of key stroke in the actual command prompt that the script is running in.

这已经在我的便携式计算机和尝试在其上运行脚本的服务器上发生.操作系统是Windows 8.1和Windows Server 2012r2,我将Anaconda 2.2与Python 3.4.3结合使用.我正在使用的唯一非标准python库是pandas 0.15.2,numpy 1.9.2,statsmodels 0.6.1和scikit-learn 0.16.1.

This has happened on my laptop and on the server that I've tried running the script on. The operating systems are Windows 8.1 and Windows Server 2012r2, I'm using Anaconda 2.2 with Python 3.4.3. The only non standard python libraries I'm using are pandas 0.15.2, numpy 1.9.2, statsmodels 0.6.1, and scikit-learn 0.16.1.

我不确定是否可以确定是否总是在特定行上发生,但是我会尝试-如果可以的话,可以将其跟踪到正在使用的特定程序包中吗?如果有人有什么想法会导致类似的事情,请分享,否则,我个人如何解决此问题的任何建议将不胜感激.

I'm not sure if I can nail down whether or not this always occurs at a specific line, but I'll try - potentially I can trace it to a particular package I'm using if I can do that? If anyone has any ideas what could cause something like this, please share, otherwise any advice on how to troubleshoot this problem on my own would greatly appreciated.

更新:我运行了以下代码以尝试重现该错误:

UPDATE: I ran the following code to try to reproduce the error:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
from sklearn.linear_model import LogisticRegression
from datetime import datetime

num_rows = 1000
i = 1

t_init = datetime.now()
while True:
    with open('temp_stage_1.txt','w') as file:
        file.write('current stage 1 iteration number: %d' % i)

    X = np.random.randint(2, size=(num_rows,25))
    y = np.random.randint(2, size=num_rows)

    with open('temp_stage_2.txt','w') as file:
        file.write('current stage 2 iteration number: %d' % i)

    clf = LogisticRegression()
    clf.fit(X,y)
    clf.score(X,y)

    with open('temp_stage_3.txt','w') as file:
        file.write('current stage 3 iteration number: %d' % i)

    logit = sm.Logit(y,X)
    results = logit.fit(disp=False)

    with open('temp_stage_4.txt','w') as file:
        file.write('current stage 4 iteration number: %d' % i)

    for j in range(10000):
        waste_time_str = 'wasting some time'

    if i % 1000 == 0:
        t_now = datetime.now()
        t_delta = (t_now-t_init).seconds
        t_init = t_now
        print(t_delta)
        print(i) 

    i += 1

我能够重现该错误,并且通过打开创建的临时文件,我可以看到在第26000次迭代中更新了第四个临时文件之后,发生了错误.我第二次运行它时,根据第四个临时文件,该错误发生在另一个1000的倍数上.另一个有趣的观察结果是,在我敲击按键并恢复执行后,打印出的时间增量反映了它坐在那里等待的时间.这也与我看到此错误的原始脚本是一致的,但是,在那种情况下,它只打印了看起来正常的时间范围,因此我知道该错误是在分配了时间值之后发生的.在这两种情况下,似乎错误都发生在其中一个打印语句上.

I was able to reproduce the error and by opening the temporary files that were created I could see that the error occurred after the 4th temporary file was updated on the 26000th iteration. I second time running it, the error occurred on another multiple of 1000 according to the 4th temporary file. Another interesting observation is that after I hit a keystroke and the execution resumes, the printed out time delta reflects the time it spent sitting there waiting. This is also consistent with the original script that I saw this error with, however, in that instance it only ever printed what appeared to be normal time ranges, so I know that the error occurred after the time values were assigned. In both cases, it looks like the error is occurring at one of the print statements.

推荐答案

您很可能偶然进入快速编辑模式"(通过在Windows终端中选择一些文本).快速编辑模式会阻止任何打印到控制台,直到您将其留下(通过按一个键)为止,这与您看到其中一条打印语句出现错误是一致的.

You are very likely entering "Quick Edit Mode" by accident (by selecting some text in the windows terminal). Quick edit mode blocks any printing to the console until you leave it (by hitting a key), which is consistent with you seeing the error occur at one of the print statements.

请参阅

See this post (not python specific) for more details.

这篇关于Python CPU使用率下降到0%,在脚本执行过程中击键后恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆