plotly / dash

Data Apps & Dashboards for Python. No JavaScript Required.
https://plotly.com/dash
MIT License
21.49k stars 2.07k forks source link

document how to stop the server on localhost #108

Open felixvelariusbos opened 7 years ago

felixvelariusbos commented 7 years ago

Hi, thank you for making this cool project!

I'm having issues with actually stopping the server on localhost. Pressing CTRL+C does not seem to actually stop it, at least not immediately. This confused me when I did something like try to run a different file only to have it appear the exact same, or close all instances/terminals that had been running Dash, go to 127.0.0.1:8050 and still see the page happily running. It doesn't seem to matter if I'm on Firefox or Chrome. This is using Git Bash on a Windows 7.

While this was going, I looked at my Task Manager and saw some stray python.exe's running even after I had completely closed the terminal. Force closing them through the Task Manager solved the problem. Sometimes they would dissipate on their own after a minute or so; not sure exactly the difference between when it stopped on its own or kept going.

I'm not sure what'd be going on other than maybe a garbage collection issue, but it made for an interesting "Am I crazy what's going on???" session.

My code is more or less following the Dash tutorials:

import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash()

x = [1,2,3,4,5,6]
y = [7,4,2,1,2,4]

app.layout = html.Div(children = [
                html.H1('Hello World'),
                html.Div('First go at making a framework.'),
                html.H2('A Toy Data Graph'),
                # a simple graph
                dcc.Graph(
                    id = 'toy-graph',
                    figure = {
                        'data': [
                            {'x': x, 'y': y, 'name': 'numbers!'}
                        ],
                        'layout': {
                            'title': 'Random numbers!'
                        }
                    }
                ),
            ])

if __name__ == '__main__':
    app.run_server(debug=True)
chriddyp commented 7 years ago

thanks for reporting @felixvelariusbos ! I haven't seen this happen myself, but I am on a different system.

I believe that this issue is in the underlying Flask server. It seems like some other folks have encountered this here: https://github.com/pallets/flask/issues/1304

Yuminzhou commented 7 years ago

I also have the same issue! Did you solve it? thanks! @felixvelariusbos

mjturtora commented 7 years ago

I'm new to plotly/dash but not to flask, found this thread from a search on same problem. fwiw, my pycharm ide can stop it (with exit code = 1) when <ctrl>-c doesn't work, but powershell can't. In ps, <ctrl>-z will stop it (it maybe even killed it but not sure).

As long as I'm here. I have another issue that perhaps I should report separately. But the reason I tried powershell in the first place is: SPEED (or lack thereof).

Including this (found it somewhere):

`app.css.config.serve_locally = True'

'app.scripts.config.serve_locally = True`

Helped with the first examples in the tutorial but not with the table example or the one after. Any hints on how to figure out what's slowing it down? I don't think it's the data reads since the slow part is after the server starts, and I'm trying to get the page to load but what do I know?

felixvelariusbos commented 7 years ago

Hey @Yuminzhou Sorry, I dropped off the face of the planet a bit. :)

I don't know if you are still having this problem, but for any newcomers on the thread, the fix for me was to turn debugging OFF

app.run_server(debug=False)

That solved the issue for me. I got that from reading the Flash issue @chriddyp referred to.

alitarraf commented 7 years ago

On a related issue, sometimes when you run and stop many time what can happen is the server will get stuck running the old version of your file. I end up stopping the port manually through TCP. It seems to me it goes into a TIME_WAIT mode before really shutting off. This picture shows what my port 8050 looks like even after Stopping the python server through Atom with CTRL C. host

chriddyp commented 6 years ago

I'm not sure how we can more robustly solve this for our users. If anyone has more details on this, please share them.

I'm adding this to our new sponsored initiative, "Dash - A Pleasant and Productive Developer Experience"

ghost commented 6 years ago

Having the same control-c problem, even with the simple server below which uses only the python core library. After I press control-c the server fails to stop and return. Then as soon as I exit chrome the server returns. I don't understand how a browser can hang a server.

`def app(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) return [b'Hello, world!']

if name == 'main': try: from wsgiref.simple_server import make_server httpd = make_server('', 8080, app) print('Serving on port 8080...') httpd.serve_forever() except KeyboardInterrupt: print('Goodbye.') ` Edit: I found a possible explanation and work around at the link below: https://stackoverflow.com/questions/25266837/ctrl-c-ends-my-script-but-it-is-not-caught-by-keyboardinterrupt-exception?rq=1

Edit: I tried the last edit without success. I noticed that Web2Py does not have the control-c problem.

felixvelariusbos commented 6 years ago

Hi @tpschmit : I don't think the browser is doing it (although I've never used the server you are talking about, so I don't know if what I'm going to say will be relevant); from what I read on the other thread, this bug is basically because in debug mode Flask makes two versions of the server, debug and real, and the control + c is only stopping one of the processes (instead of both). It looks like the browser is bringing it back to life because it never really died in the first place.

As an aside, as I've continued developing around this issue, I've noticed that if my server died because of an error (for example syntax), it ends cleanly. It's pretty much only when pressing control+c does this zombie buddy stick around.

ttfreeman commented 6 years ago

Any solution to this? I am having the same problem

chriddyp commented 6 years ago

@User8621 - Could you share more information about your environment?

ttfreeman commented 6 years ago

I already found a solution to this problem by going to Windows Task Manager and killing Python task. However, just to have answered your questions: Operating system: Windows 10 Home Edition What environment are you running the Dash command in (terminal, jupyter notebook, windows bash, etc)? VS Code What command line instruction are you running? if name == 'main': app.run_server(debug=True or False doesn't matter in server problem)

Can you comfirm that cntrl-c does not work? Confirming that cnrl-C did not work.

chriddyp commented 6 years ago

What environment are you running the Dash command in (terminal, jupyter notebook, windows bash, etc)? VS Code

That's odd. VS Code's documentation says:

Stop the app by using Ctrl+C in the terminal.

See here for a reference: https://code.visualstudio.com/docs/python/tutorial-flask

Dmancuso1 commented 4 years ago

I did a hard refresh - cmd (or ctrl) +shift+r - on my browser and it seemed to close the connection.

Nermin-Ghith commented 4 years ago

False

I had a similar issue and I also solved it by turning OFF the debugging function by the end of the script:

app.run_server(debug=False)

Sakshi29101999 commented 4 years ago

I am not facing such a problem but instead, I am not getting a solution to this problem.

How to close the opened browser after my work is complete from the Web Page, it should also close/kill the script running in the background. (Note- can not use ctrl+C for it)

Anybody can help!!!

henrytirla commented 4 years ago

I am using a mac i just used the activity monitor search for python and killed it. Then started my other Dash application and it worked fined. It's not the best method but it saved me time when i had to focus on my project work.

DamienSn commented 4 years ago

I did a hard refresh - cmd (or ctrl) +shift+r - on my browser and it seemed to close the connection.

I had a similar problem (Server doesn't stop) and do a hard refresh solved it. But i think it wasn't not the server who doesn't stop, it's the navigator who had indexed files and show a copy of them.

Kamalabot commented 3 years ago

The hard refresh using Ctrl+shift+r solves the server stopping issue.

Another point is, the server is also slow when it is first serving. There is atleast 2 seconds of delay for the page to come up as the complexity of the page increases.

It could be because of "Lazy Loading" by Flask.

marvinseifert commented 2 years ago

One solution that works on Windows is to open cmd, type netstat -ano | findstr :8050 (if localhost 8050 was used). Identify which process is listening: LISTENING 28380 and than type: taskkill /PID 28380 /F

CleberAP commented 1 year ago

Other solution, I am using Windows 7. type : taskkill /f /im pythonw.exe

It wasn't enough to kill the LISTENING process. It was necessary to kill the script.py process that started the application, and it was necessary to identify it first. So to make it easier, I killed all processes with the above command.

I used the DASH library, which runs with flask. Even running directly through cmd, 2 PID will be started.

If running with IDLE python, even finalizing the PID of the Dash, it is necessary to finalize the PID of the script started in IDLE.

By CMD, when the PID of the Dash is finalized, the PID of the IDLE will also be finalized.

gvwilson commented 5 months ago

@LiamConnors should we add a note about this to our README or documentation?