簡體   English   中英

Python CPU使用率降至0%,在腳本執行期間按鍵后恢復

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

我的問題幾乎與此處發布的問題相同:

Python一直睡到鍵擊

這個帖子多年來一直處於非活動狀態,如果有一個不同的協議“重新開放”這個問題請告訴我 - 我同時發布這個問題,如果我應該這樣做,我會提前道歉。

我無法發布代碼,但這里有一些我可以分享的細節 - 我正在執行一個包含許多迭代生成的print語句的腳本,以跟蹤腳本執行的幾個小時內的進度。 在任務管理器中監視我的CPU使用情況時,我可以看到,當我在運行腳本的實際命令提示符中輸入任何類型的鍵擊時,周期性地使用率降至0%並且僅恢復。

這發生在我的筆記本電腦和我試過運行腳本的服務器上。 操作系統是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。

我不確定我是否可以確定這是否總是發生在特定的一行,但我會嘗試 - 如果我能做到這一點,我可以將它追蹤到我正在使用的特定包裹中嗎? 如果有人有什么想法可能導致這樣的事情,請分享,否則任何關於如何解決這個問題的建議將非常感謝。

更新:我運行以下代碼以嘗試重現錯誤:

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次迭代更新第4個臨時文件后發生了錯誤。 我第二次運行它時,根據第4個臨時文件,錯誤發生在1000的另一個倍數上。 另一個有趣的觀察是,在我按下按鍵並恢復執行后,打印出的時間增量反映了坐在那里等待的時間。 這也與我看到此錯誤的原始腳本一致,但是,在該實例中它只打印了看似正常時間范圍的內容,所以我知道錯誤發生在分配時間值之后。 在這兩種情況下,看起來錯誤發生在其中一個打印語句中。

您很可能偶然進入“快速編輯模式”(通過在Windows終端中選擇一些文本)。 快速編輯模式會阻止對控制台的任何打印,直到您離開它(通過按鍵),這與您在其中一個打印語句中看到錯誤一致。

有關更多詳細信息,請參閱此文章 (不是特定於python)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM