# Solving JuMP model async with HTTP routing doesn't work

**URL:** <https://discourse.julialang.org/t/solving-jump-model-async-with-http-routing-doesnt-work/65854>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, http\
**Created:** [August 5, 2021, 4:13am UTC](https://discourse.julialang.org/t/solving-jump-model-async-with-http-routing-doesnt-work/65854 "2021-08-05T04:13:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![obar](https://avatars.discourse-cdn.com/v4/letter/o/a9adbd/32.png) [@obar](https://discourse.julialang.org/u/obar)\
**Post date:** [August 5, 2021, 4:13am UTC](https://discourse.julialang.org/t/solving-jump-model-async-with-http-routing-doesnt-work/65854/1 "2021-08-05T04:13:37Z")

</div>

I’m in the process of building a web server that solves a mixed-integer program. The solve is triggered when the user hits a specific url. Because the MIP solve takes a while to complete, this causes a timeout in the HTTP request. Ideally, we’d run the solve asynchronously and send back an immediate response that the solve started. However, it seems that the `@async` macro simply does not work inside the router if a JuMP model is optimized inside it.

Example code:

```julia
using HTTP, Cbc, JuMP, Sockets

function async_solve(request::HTTP.Request)

    @async begin
        println("async solve started")

        model = read_from_file("model.mps")
        set_time_limit_sec(model, 10)
        set_optimizer(model, Cbc.Optimizer)
        optimize!(model)

        println("async solve complete")
    end

    return HTTP.Response(200, "Done")
end

ROUTER = HTTP.Router()
HTTP.@register(ROUTER, "/", async_solve)
HTTP.serve(ROUTER, Sockets.localhost, 8080)

```

Note: any large enough MIP will do the job as the .mps file - let me know if you want me to provide a copy for testing, I cannot seem to upload it here directly.

Once the server is running, go to localhost:8080 in your browser and you will see the cbc logs running in the REPL while the page is still loading. If the macro functioned correctly, we’d expect to see “Done” in the browser window long before the Cbc solve finishes

We can try replace the JuMP bits with `sleep(10)` to see what happens when running other code in the `@async` block:

```julia
using HTTP, Cbc, JuMP, Sockets

function async_solve(request::HTTP.Request)

    @async begin
        println("async solve started")

        sleep(10)

        println("async solve complete")
    end

    return HTTP.Response(200, "Done")
end

ROUTER = HTTP.Router()
HTTP.@register(ROUTER, "/", async_solve)
HTTP.serve(ROUTER, Sockets.localhost, 8080)

```

When a browser goes to localhost:8080, it immediately displays “Done” and the REPL shows “async solve complete” after about 10 seconds as expected.

To clarify, when solving a JuMP model inside `@async` without the HTTP router, it works as expected  
I am running Julia v1.60, JuMP v0.21.6, HTTP v0.9.12  
Not too sure if this is a JuMP or HTTP issue, please advise if more information is needed here.

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [August 5, 2021, 5:26am UTC](https://discourse.julialang.org/t/solving-jump-model-async-with-http-routing-doesnt-work/65854/2 "2021-08-05T05:26:05Z")

</div>

You should start process on a different thread with @spawn.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [August 5, 2021, 5:41am UTC](https://discourse.julialang.org/t/solving-jump-model-async-with-http-routing-doesnt-work/65854/3 "2021-08-05T05:41:23Z")

</div>

I don’t know the details, perhaps someone more knowledgeable can chime in, but I assume the call to Cbc in the solve is blocking.

The suggestion for spawn is to start Julia with `julia -t 2` (or more threads if you want), then use something like:

```nohighlight
using HTTP, Cbc, JuMP

function solve_model()
    model = Model(Cbc.Optimizer)
    @variable(model, x[1:10], Bin)
    @objective(model, Max, rand(10)' * x)
    @constraint(model, rand(10)' * x <= 5)
    optimize!(model)
    return objective_value(model)
end

function async_solve(request::HTTP.Request)
    Threads.@spawn begin
        println("async solve started")
        cost = solve_model()
        println("async solve complete. Cost = $(cost)")
    end
    return HTTP.Response(200, "Done")
end

ROUTER = HTTP.Router()
HTTP.@register(ROUTER, "/", async_solve)
HTTP.serve(ROUTER, "127.0.0.1", 8080)

```

---

<div class="post-metadata">

**Author:** ![obar](https://avatars.discourse-cdn.com/v4/letter/o/a9adbd/32.png) [@obar](https://discourse.julialang.org/u/obar)\
**Post date:** [August 5, 2021, 5:57am UTC](https://discourse.julialang.org/t/solving-jump-model-async-with-http-routing-doesnt-work/65854/4 "2021-08-05T05:57:38Z")

</div>

Thanks a lot! That works perfectly.
