英文:
How to run web server (Warp) in async/concurrent mode?
问题
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 as well.
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.
英文:
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
得分: 2
不要允许主线程终止。应该像这样工作:
 do
   a1 <- Async.async $ Warp.run 3000 waiApp
   a2 <- Async.async $ Warp.run 3002 waiApp
   ...
   Async.waitAny [a1, a2]
英文:
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]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论