简体   繁体   English

Python CPU使用率降至0%,在脚本执行期间按键后恢复

[英]Python CPU usage drops to 0%, resumes after keystroke during script execution

my issue is nearly identical to the issue posted here: 我的问题几乎与此处发布的问题相同:

Python sleeps until keystroke 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. 这个帖子多年来一直处于非活动状态,如果有一个不同的协议“重新开放”这个问题请告诉我 - 我同时发布这个问题,如果我应该这样做,我会提前道歉。

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. 我无法发布代码,但这里有一些我可以分享的细节 - 我正在执行一个包含许多迭代生成的print语句的脚本,以跟踪脚本执行的几个小时内的进度。 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. 在任务管理器中监视我的CPU使用情况时,我可以看到,当我在运行脚本的实际命令提示符中输入任何类型的键击时,周期性地使用率降至0%并且仅恢复。

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. 操作系统是Windows 8.1和Windows Server 2012r2,我使用的是Anaconda 2.2和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. 我使用的唯一非标准python库是pandas 0.15.2,numpy 1.9.2,statsmodels 0.6.1和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

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. 我能够重现错误并打开创建的临时文件,我可以看到在第26000次迭代更新第4个临时文件后发生了错误。 I second time running it, the error occurred on another multiple of 1000 according to the 4th temporary file. 我第二次运行它时,根据第4个临时文件,错误发生在1000的另一个倍数上。 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. 在这两种情况下,看起来错误发生在其中一个打印语句中。

You are very likely entering "Quick Edit Mode" by accident (by selecting some text in the windows terminal). 您很可能偶然进入“快速编辑模式”(通过在Windows终端中选择一些文本)。 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)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM