How do I kill a dash app automatically after n seconds?
Wow weird question. When does the timer need to start? On creating the server or after first request?
Also: why?
I agree that this doesn’t really make sense as a thing to do. What is your use case? Can it be solved in a better way than app suicide?
If you really need to do it…
import os
import time
# get the pid when launching the app
app_pid = os.getpid()
# how many seconds the app should run for
kill_seconds = 500
# start the timer
app_start = time.time()
... # app code including an interval
@app.callback(
Output('arbitrary-div','children'),
Input('interval thing','n_intervals')
)
def kill_app(n):
if time.time() - start >= kill_seconds:
os.system("kill {}".format(str(app_pid)) # this kills the app
Hey! Sorry for being incomplete.
My exact use case is to kill a dash app automatically after 7 days or let’s say after n seconds.
Right now, I have to do it manually via Ctrl+C.
Just wanted a way to kill it.
The timer needs to start just after the server is created.
Hey!
This thing isn’t helping me close the dash app in the command prompt.
Any other work around?
import dash
import dash_html_components as html
import multiprocessing
import time
app = dash.Dash(__name__)
app.layout = html.Div([
html.P("Hello World")
])
if __name__ == '__main__':
p = multiprocessing.Process(target=app.run_server, name="Foo", kwargs=dict(debug=False))
p.start()
time.sleep(10)
# End process
p.terminate()
p.join()
Thanks @sjtrny
This code is giving the following error
Traceback (most recent call last):
File "C:\Users\AJain7\Desktop\main_2.py", line 69, in <module>
p.start()
File "C:\Users\AJain7\AppData\Local\Programs\Python\Python36\lib\multiprocessing\process.py", line 105, in start
self._popen = self._Popen(self)
File "C:\Users\AJain7\AppData\Local\Programs\Python\Python36\lib\multiprocessing\context.py", line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:\Users\AJain7\AppData\Local\Programs\Python\Python36\lib\multiprocessing\context.py", line 322, in _Popen
return Popen(process_obj)
File "C:\Users\AJain7\AppData\Local\Programs\Python\Python36\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__
reduction.dump(process_obj, to_child)
File "C:\Users\AJain7\AppData\Local\Programs\Python\Python36\lib\multiprocessing\reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
AttributeError: Can't pickle local object 'Dash.init_app.<locals>._handle_error'
C:\Users\AJain7>Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\AJain7\AppData\Local\Programs\Python\Python36\lib\multiprocessing\spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "C:\Users\AJain7\AppData\Local\Programs\Python\Python36\lib\multiprocessing\spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
EOFError: Ran out of input
any ideas around it?
Thanks you!
Did you copy the code exactly? The repl.it below shows that it works.
Check that you have the latest versions of the required packages? Can you post the code you are using here?
The example above works, so either your configuration or code is different.
This is the exact code that I am using.
Python version - 3.6.6
dash version - 1.3.1
import dash
import dash_html_components as html
import multiprocessing
import time
app = dash.Dash(__name__)
app.layout = html.Div([
html.P("Hello World")
])
if __name__ == '__main__':
p = multiprocessing.Process(target=app.run_server, name="Foo", kwargs=dict(debug=False,host="0.0.0.0"))
p.start()
time.sleep(10)
# End process
p.terminate()
p.join()
Was there a resolution to this? I have the same issue and have tried the example that was posted with exactly the same error message resulting.
I am running python 3.7.6 and dash 1.13.1 on Windows. Could this be an issue with python/dash on Windows? My colleague gets no errors on MacOS.
Thanks.