Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stopping script with Ctrl+C in the console is not working #8051

Open
arnyeinstein opened this issue Oct 11, 2018 · 18 comments
Open

Stopping script with Ctrl+C in the console is not working #8051

arnyeinstein opened this issue Oct 11, 2018 · 18 comments

Comments

@arnyeinstein
Copy link

Hi
I am running the following dash script. Stopping the server with Ctrl-C in the console is not working on my Windows 10, Python 3.7.0 machine using spyder 3:
` # -- coding: utf-8 --
import dash
import dash_core_components as dcc
import dash_html_components as html

# external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__)
app.layout = html.Div(children=[
    html.H1(children='Example Dash'),

    html.Div(children='''
        Dash: A web application framework for Python.
    '''),

    dcc.Graph(
        id='example-graph',
        figure={
            'data': [
                {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
                {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
            ],
            'layout': {
                'title': 'Dash Data Visualization'
            }
        }
    )
])

if __name__ == '__main__':
    app.run_server()

`
I replaced Version 3 by the development version 4 where Ctrl-C is working as expected.

Cheers
Renger

@CAM-Gerlach
Copy link
Member

@ccordoba12 Is this not a duplicate of #8013 ?

@ccordoba12
Copy link
Member

I think this is different and it has to do with not being able to stop Flask apps.

@talkaminker
Copy link
Contributor

I have the same problem on win10 python 3.7 even for simple scripts like:

for i in range(10):
   time.sleep(1)

on python 3.6 it works fine

@CAM-Gerlach
Copy link
Member

@talkaminker This isn't the same problem as the one reported above; the issue you are having is the same as the one I linked previously, #8013 . It has to do with third party dependency versions; try the solutions suggested over there which fixed it for the reporter on that thread. Thanks.

@joarvatnaland
Copy link

Has anyone found a solution or workaround to this problem. I am scratching my head here to stop my python flask app on port 8080.

@ccordoba12 ccordoba12 modified the milestones: important, v4.0beta3 Apr 26, 2019
@ccordoba12
Copy link
Member

For now I think the only possibility is to restart the kernel. We'll take a look at this in Spyder 4 to see if we can improve the situation.

@ccordoba12 ccordoba12 modified the milestones: v4.0beta3, v4.0beta4 May 18, 2019
@ccordoba12 ccordoba12 modified the milestones: v4.0betaX, 4.0.1 Aug 10, 2019
@ccordoba12 ccordoba12 modified the milestones: 4.1.0, important Dec 12, 2019
@eervin123
Copy link

Hello all. I'm not sure if any resolution was made on this. I love the Spyder and everything you all are doing. I'm a noob so forgive me if I'm cluttering up the message board, but I am experiencing similar issues with this and I thought I could at least share the results in order to help you diagnose the problem. For reference, I am working with dash (which ultimately runs a flask app).

I'm not sure if this will assist in diagnosing the problem. Again, I'm not looking for help on this, I can just restart the kernel as the system gets bogged down, but I just wanted to share the issue in the event that it helps you.

Spyder version 4.1.3
Python 3.7.7 64-bit | Qt 5.9.6 | PyQt5 5.9.2 | Windows 10

Description of my script: It is a very basic Dash DataTable script to play around with the demo. The dataframe is about 6 columns and 8 rows. The data is static. The entire app is static. It is not clickable, editable, or anything. (This may be informative later on when you see how much CPU usage was hogged when I ran and stopped it several times.)

In order to run the app in "debug mode" I had to set "use_reloader = False" otherwise it would crash and not run the app. Not sure if that is a Spyder thing or Dash or my machine.

Calling the app.

if __name__ == '__main__':
    app.run_server(debug=True, use_reloader=False)

Console output after running the app one time.
image

Killing the app.
Ctrl-C works fine to stop the server. However, when I make some changes and run the app again, it appears now two servers are running, I did this a few times just to watch confirm, and without fully restarting the kernel, this continues to add new processes. Console output listed out at the bottom here.

Console output after running once, killing the app using Ctrl-C, then running it again.
image

Task manager after running and stopping and running and stopping several times.
image

Task manager after restarting the kernel (Spyder still open but kernel is dormant). It is interesting that all of the tasks are still listed and the memory allocated is somewhat large but the CPU went back to zero.
image

Here is an image of the task manager after running and stopping 3 times. CPU usage now around 8-9% only one python process occupying the CPU. (Recall that the app is not dynamic, it is just displaying static data to a browser.)
image

In the console, after restarting the kernel and running the script, it only lists one app running
here is an image of that.
image

Below is the output from running and stopping several times.

This is the console output after running the app and stopping then re-running it several times.  
Dash is running on http://127.0.0.1:8050/

Dash is running on http://127.0.0.1:8050/

Dash is running on http://127.0.0.1:8050/

Dash is running on http://127.0.0.1:8050/

Dash is running on http://127.0.0.1:8050/

Dash is running on http://127.0.0.1:8050/

Dash is running on http://127.0.0.1:8050/

Dash is running on http://127.0.0.1:8050/

Dash is running on http://127.0.0.1:8050/

Dash is running on http://127.0.0.1:8050/

 Warning: This is a development server. Do not use app.run_server
 Warning: This is a development server. Do not use app.run_server
 Warning: This is a development server. Do not use app.run_server
 Warning: This is a development server. Do not use app.run_server
 Warning: This is a development server. Do not use app.run_server
 Warning: This is a development server. Do not use app.run_server
 Warning: This is a development server. Do not use app.run_server
 Warning: This is a development server. Do not use app.run_server
 Warning: This is a development server. Do not use app.run_server
 Warning: This is a development server. Do not use app.run_server
 in production, use a production WSGI server like gunicorn instead.

 in production, use a production WSGI server like gunicorn instead.

 in production, use a production WSGI server like gunicorn instead.

 in production, use a production WSGI server like gunicorn instead.

 in production, use a production WSGI server like gunicorn instead.

 in production, use a production WSGI server like gunicorn instead.

 in production, use a production WSGI server like gunicorn instead.

 in production, use a production WSGI server like gunicorn instead.

 in production, use a production WSGI server like gunicorn instead.

 in production, use a production WSGI server like gunicorn instead.

 * Serving Flask app "index_perf_table" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on


@WarwickShipway
Copy link

Same issue with ctrl + c in spyder console not stopping the windows api, when running flask apps. I typically ctrl + . as stated above or create a system exit by adding some failure code and wait for the refresh.

@SwethA99hub
Copy link

SwethA99hub commented Aug 15, 2022

Hello I am working on spyder for web application I am also facing same issue ctrl+c isn't helping to quite the current directory any help please how to resolve .

That's my console saying:-

  • Restarting with fsevents reloader
  • Debugger is active!
  • Debugger PIN: 120-008-501
    127.0.0.1 - - [15/Aug/2022 20:44:52] "GET /user/Loki HTTP/1.1" 200 -
    127.0.0.1 - - [15/Aug/2022 20:45:01] "GET /user/Loki HTTP/1.1" 200ger is active!
  • Debugger PIN: 120-008-501

@spyder-ide spyder-ide deleted a comment from SwethA99hub Aug 15, 2022
@ccordoba12 ccordoba12 modified the milestones: important, v5.3.4 Aug 15, 2022
@ccordoba12
Copy link
Member

@impact27, any ideas about why KeyboardInterrupt is not working for web apps?

I was also thinking that perhaps we could fix this with the improvements you made to interrupt the kernel in master of Spyder-kernels (not sure about it though).

@ccordoba12 ccordoba12 changed the title Stopping script with Ctrl-C in the console is not working Stopping script with Ctrl+C in the console is not working Aug 16, 2022
@CAM-Gerlach
Copy link
Member

I'm very much not a WSGI/web server expert, but if I were to guess, either one or both of the following may occurring:

  • Flask is spawning subprocesses to handle the actual web requests, which are not getting killed properly, and/or
  • Flask is trapping the either the actual keyboard interrupt signal, or the KeyboardInterruptError Python-level exception, and instead of exiting, it instead relaunches new subprocesses in debug mode

Then, when it is run again, it launches another set of them.

@SwethA99hub
Copy link

Does that have anything to worry with application update 🤔🤔🤔 the sypder I am working version is 5.0.0 .

@CAM-Gerlach
Copy link
Member

Unlikely, but not impossible; you should test and confirm with the latest version, 5.3.2—5.0.0 is a year and a half out of date, and as the very first release of the 5.x series, contained a number of subsequently-fixed bugs. Also, your Spyder-Kernels version in your working environment is perhaps more important here than the Spyder version.

@SwethA99hub
Copy link

Hey , thank you so much for helping as my system is Macos , it ain't returning results with ctrl+c , to quit console use **ctrl + . **

@impact27
Copy link
Contributor

I haven't investigated this particular issue, but in general, handling KeyboardInterruptis the user code responsability, so there is little we can do. For example the following code can not be interrupted by spyder:

import time
while True:
    try:
        time.sleep(1)
    except:
        # I will catch any exception and BaseException, inculding KeyboardInterrupt
        pass

I would recommand restarting the kernel instead of using keyboard interrupt

@CAM-Gerlach
Copy link
Member

CAM-Gerlach commented Aug 16, 2022

For example the following code can not be interrupted by spyder:

Or, to be clear, interrupted by the user when running from a standard python interpreter in the terminal, except via hard-killing the process with Ctrl-Pause/Break, Ctrl-\, etc. However, if there is different behavior between inside Spyder and in QtConsole, or at least ipython, then that perhaps should be investigated.

@impact27
Copy link
Contributor

However, if there is different behavior between inside Spyder and in QtConsole, or at least ipython, then that perhaps should be investigated.

I completely agree

@CAM-Gerlach
Copy link
Member

@SwethA99hub , could you run jupyter qtconsole in your working environment and try to reproduce your issue in there? What about if you run ipython?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants