4

my issue is nearly identical to the issue posted here:

Python sleeps until keystroke

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. 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.

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

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.

Community
  • 1
  • 1
user3749714
  • 183
  • 2
  • 9
  • We may not need your exact code, but it would be very helpful to have code that duplicates the issue. – Alyssa Haroldsen Aug 05 '15 at 19:00
  • Do you have any other python scripts running at the same time? Because in python there is the GIL (global interpreter lock) that "prevents multiple native threads from executing Python bytecodes at once" (https://wiki.python.org/moin/GlobalInterpreterLock). So, maybe there is some type of race condition there and when you type the keyboard, that python process get a sudden boost in priority on the cpu and starts computing again. Note that threading.thread(s) are also subject to the GIL. – muchwow Aug 05 '15 at 19:11
  • I have seen this happen in the interactive interpreter - I'll be moving along as normal, and then I enter a statement and nothing happens until I enter a keystroke. Other statements work fine, but that particular one doesn't proceed without a keystroke. I don't know what causes it, but closing the interpreter and opening a new one seems to resolve it. I can then execute the problematic statement without issue. – TigerhawkT3 Aug 05 '15 at 19:12
  • @Kupiakos I will attempt to write some generic code to reproduce the issue. – user3749714 Aug 05 '15 at 19:19
  • @jumojer I do have other python instances running while the script is executing. At any given time I could have several IPython sessions or IPython notebooks open doing various things. It would be somewhat problematic if I can't do this, but if this is the problem then maybe I can find a work around. – user3749714 Aug 05 '15 at 19:22
  • Have you tried pressing ctl-C or break or whatever it is in Windows these days to cause a keyboard interrupt and looking at the stack trace? – Patrick Maupin Aug 06 '15 at 00:52
  • @PatrickMaupin I've tried pressing ctl-C, but it actually doesn't immediately produce the trace back, it registers the keystroke and resumes execution - once it's resumed, if I press ctl-C again it does produce the trace back, but I don't think I could use this, for example, to see what line of code I'm getting hung up on because I can't say for sure that I have any hope of stopping it immediately upon resumption of execution. – user3749714 Aug 06 '15 at 17:28
  • Does the issue only happen in IPython? I don't know much about that shell, but it might be the source of the issue, rather than your Python code itself. – Blckknght Aug 06 '15 at 19:02
  • @Blckknght no, it happens outside of ipython as well. Although since I've been running on Windows, my preference has been to use the Anaconda command prompt that comes with the installation. – user3749714 Aug 06 '15 at 19:05

1 Answers1

0

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.

mbrig
  • 929
  • 12
  • 16