# Hot restart of the main loop when the source code changes

**URL:** <https://discourse.julialang.org/t/hot-restart-of-the-main-loop-when-the-source-code-changes/38840>\
**Category:** General Usage\
**Created:** [May 5, 2020, 10:22pm UTC](https://discourse.julialang.org/t/hot-restart-of-the-main-loop-when-the-source-code-changes/38840 "2020-05-05T22:22:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![waralex](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/waralex/32/13415_2.png) [@waralex](https://discourse.julialang.org/u/waralex)\
**Post date:** [May 5, 2020, 10:22pm UTC](https://discourse.julialang.org/t/hot-restart-of-the-main-loop-when-the-source-code-changes/38840/1 "2020-05-05T22:22:44Z")

</div>

Hi, everybody!  
I’m currently working on the Julia [backend](https://github.com/plotly/Dash.jl) for [Plotly Dash](https://plotly.com/dash/) and I’m faced with the following task. In Python and R, Dash has the following feature: if the server is running in debug mode, changes to the app’s source code will restart the server. This way, you don’t need to press ctrl-C in the terminal after each change and restart the application.  
I’m trying to implement this for Julia and the first my experiments gave this draft solution (spied on Python’s flask):

```julia
function test_hot_restart(main_loop::Function)
    if haskey(ENV, "SUBPROCESS")
        app_path = abspath(Base.PROGRAM_FILE)
        files = FileStat[]
        parse_includes(app_path, files) #parse files recursively with Base.parse_input_line to find include Exprs
        
        @async main_loop()

        poll_until_changed(files) #Waiting for changes in any of the found files
        exit(3)
    else
        julia_str = joinpath(Sys.BINDIR, Base.julia_exename())
        new_env = deepcopy(ENV)
        new_env["SUBPROCESS"] = "1"
        cmd = Cmd(`$(julia_str) $(app_path)`, env = new_env, ignorestatus = true)
        while true
            p = run(cmd)
            p.exitcode != 3 && break
        end
    end
end

```

This solution seems to work, but perhaps someone can offer a more robust one?

P.S.  
For a better understanding of the problem, the source file that I need to track for changes looks in the simplest case something like this:

```julia
using Dash, DashHtmlComponents
app = dash()
app.layout = html_div("test")
run_server(app; debug = true)

```

the main loop is started inside run\_server (by `HTTP.serve` call). So I don’t see a way to use `Revise.jl` here. In addition, there is a requirement that the hot restart mode is enabled in runtime by the `debug` argument, and not by a macro or imported an extra package in user side.

---

<div class="post-metadata">

**Author:** ![caleb-allen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caleb-allen/32/14054_2.png) [@caleb-allen](https://discourse.julialang.org/u/caleb-allen)\
**Post date:** [March 5, 2021, 1:49am UTC](https://discourse.julialang.org/t/hot-restart-of-the-main-loop-when-the-source-code-changes/38840/2 "2021-03-05T01:49:31Z")

</div>

Hey @waralex, were you able to find a more robust solution here? I’m running up against a similar problem and was hoping to find a nice `Revise` based solution

---

<div class="post-metadata">

**Author:** ![waralex](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/waralex/32/13415_2.png) [@waralex](https://discourse.julialang.org/u/waralex)\
**Post date:** [March 5, 2021, 6:48am UTC](https://discourse.julialang.org/t/hot-restart-of-the-main-loop-when-the-source-code-changes/38840/3 "2021-03-05T06:48:23Z")

</div>

Hi @caleb-allen!  
Unfortunately, I didn’t find it ☹ In Dash, it now works like this - it looks at all the included files and restarts the server when any of it change.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 5, 2021, 7:26am UTC](https://discourse.julialang.org/t/hot-restart-of-the-main-loop-when-the-source-code-changes/38840/4 "2021-03-05T07:26:42Z")

</div>

In “FemotCleaner” (a bot that updates Julia code from 0.7 to 1.0) there was a `Revise.revise()` call in the main loop:

> <https://github.com/JuliaComputing/FemtoCleaner.jl/blob/384815c983609275ae8bdc0aa0dcc67a287d1768/src/FemtoCleaner.jl#L508-L512>

From what I recall, it worked pretty well, assuming the changes were Revise compatible.

---

<div class="post-metadata">

**Author:** ![caleb-allen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caleb-allen/32/14054_2.png) [@caleb-allen](https://discourse.julialang.org/u/caleb-allen)\
**Post date:** [March 8, 2021, 5:22pm UTC](https://discourse.julialang.org/t/hot-restart-of-the-main-loop-when-the-source-code-changes/38840/5 "2021-03-08T17:22:30Z")

</div>

@kristoffer.carlsson this is great! I’ve tried using `Revise.revise()` from within code, but I hadn’t thought to use `Base.invokelatest`. I’ll give this a shot!

---

<div class="post-metadata">

**Author:** ![caleb-allen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caleb-allen/32/14054_2.png) [@caleb-allen](https://discourse.julialang.org/u/caleb-allen)\
**Post date:** [March 10, 2021, 5:43pm UTC](https://discourse.julialang.org/t/hot-restart-of-the-main-loop-when-the-source-code-changes/38840/6 "2021-03-10T17:43:30Z")

</div>

@kristoffer.carlsson that did the trick! `invokelatest` was the key. Thanks for the suggestion!
