0

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.

WHITECOLOR
  • 24,996
  • 37
  • 121
  • 181
  • 2
    In your last example: just remove the second forkIO, so that your main thread doesn't terminate right away, but is busy running the second server. – Artem Pelenitsyn Feb 13 '23 at 22:10
  • Ah, thanks, and if I want to keep the second forkIO and let the code below execute, what better way to just make the main thread run forever? – WHITECOLOR Feb 13 '23 at 22:13
  • The best way is to give it something to do like running a server, or checking that all server threads still run... Everything else is just a waste of the threads resources. – cafce25 Feb 13 '23 at 22:26
  • @cafce25 You mean say `forever (threadDelay $ 1000 * 1000)` is not a good solution? – WHITECOLOR Feb 13 '23 at 22:36
  • Doing useless computation forever does not seem very good no. – cafce25 Feb 13 '23 at 22:41
  • Ok, I made with `Async.wait`, probably, it is correct solution for my case. – WHITECOLOR Feb 13 '23 at 22:48

1 Answers1

2

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]
WHITECOLOR
  • 24,996
  • 37
  • 121
  • 181