How to run web server (Warp) in async/concurrent mode?

109 Views Asked by At

I'm using https://hackage.haskell.org/package/warp-3.3.24/docs/Network-Wai-Handler-Warp.html

I don't know much about haskell concurrency. Say I would like to run two servers on different ports:

So I do:

 do
   Warp.run 3000 waiApp
   Warp.run 3002 waiApp

Then server is run on 3000 is working, but it never gets to the next line.

I tried:

 do
   forkIO $ Warp.run 3000 waiApp
   forkIO $ Warp.run 3002 waiApp

But it doesn't seem to work, each of them just stop after forking.

How to make it work properly? Also I want to allow the code below to be executed aslo.

UPD:

So the current solution is just to add

forever (threadDelay 1000)

in the end of the main, I wonder if it is the correct way to do this.

1

There are 1 best solutions below

0
WHITECOLOR On

So, we should not allow the main thread to terminate. Something like this should work:

 do
   a1 <- Async.async $ Warp.run 3000 waiApp
   a2 - Async.async $ Warp.run 3002 waiApp
   ...
   Async.waitAny [a1, a2]