plotly-resampler icon indicating copy to clipboard operation
plotly-resampler copied to clipboard

Stop_Server() Is not working to close the server when show_dash() is called in a separate thread

Open Jamesong7822 opened this issue 2 years ago • 2 comments

Hi, here's a MWE to reproduce this issue: Here are the module versions:

  1. plotly-resampler==0.8.3.1
  2. Flask==2.1.3
  3. jupyter-dash==0.4.2
  4. Werkzeug==2.1.2
import plotly.graph_objects as go; import numpy as np
from plotly_resampler import FigureResampler
import time
import threading

x = np.arange(1_000_000)
sin = (3 + np.sin(x / 200) + np.random.randn(len(x)) / 10) * x / 1_000

fig = FigureResampler(go.Figure())
fig.add_trace(go.Scattergl(name='noisy sine', showlegend=True), hf_x=x, hf_y=sin)

threading.Thread(target=fig.show_dash, kwargs={"mode": "external", "port":8050}, daemon=True).start()

start_time = time.time()
while True:
    if time.time() - start_time > 10:
        print("Time to stop!")
        fig.stop_server()
        print("Stop server ran!")
        break

# This loop is meant to keep the script alive
while True:
    pass

If the fig.stop_server() function is working, refreshing the browser: 127.0.0.1:8050 should yield a "SITE CANNOT BE REACHED ERROR" which is expected if the dash server is functional.

However, that is not the case, and the server is still alive. Please kindly advise, how one can go about terminating the server, without having to terminate the entire python script.

Jamesong7822 avatar Mar 23 '23 00:03 Jamesong7822

Hey @Jamesong7822,

Thx for submitting this issue together with the qualitative reproducible code :)

I ran this code on my machine, but could not reproduce the bug (the server stops after 10s, and I get this This site can’t be reached in the browser when I reload).

Are you by any chance working on Windows? I'm not an expert on this, but I do know that multiprocessing (& possibly threading) on Windows is performed differently than on Unix systems...

Hope to get this sorted out!

Cheers, Jeroen

P.S.: my apologies for the late response - had some paper deadlines that limited my spare time (to work on open-source contributions)

jvdd avatar Apr 04 '23 07:04 jvdd

I further looked into this, and it appears that this code works as-expected when running this within a Jupyter cell (using Ipython kernel); this activates the JupyterDash component of Dash which allows (for some yet unknown reason, to behave as expected).

If you use this as a python script, this code does not work as expected, and the just-started server is not found in the server_dict and can thus be not killed.

This issue thus needs further follow-up.

jonasvdd avatar Jul 04 '23 12:07 jonasvdd