如何以异步/并发模式运行Web服务器(Warp)?

huangapple go评论49阅读模式
英文:

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 &lt;- Async.async $ Warp.run 3000 waiApp
   a2 - Async.async $ Warp.run 3002 waiApp
   ...
   Async.waitAny [a1, a2]

huangapple
  • 本文由 发表于 2023年2月14日 06:05:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定