Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement main server loop in Haskell?

Tags:

haskell

What is the generally accepted way to implement the main loop of a server that needs to wait on a heterogeneous set of events? That is the server should wait (not busywait) until one of the following occurs:

  • new socket connection
  • data available on an existing socket
  • OS signal
  • third-party library callbacks
like image 843
brooks94 Avatar asked Sep 13 '12 19:09

brooks94


People also ask

Does Haskell have for loops?

Recursion is important to Haskell because unlike imperative languages, you do computations in Haskell by declaring what something is instead of declaring how you get it. That's why there are no while loops or for loops in Haskell and instead we many times have to use recursion to declare what something is.

Does Haskell have stack overflow?

There is no call stack in Haskell. Instead we find a pattern matching stack whose entries are essentially case expressions waiting for their scrutinee to be evaluated enough that they can match a constructor (WHNF).


1 Answers

I think you're thinking in terms of a C paradigm with a single thread, nonblocking I/O, and a select() call.

You can manage to write something like that in Haskell, but Haskell has much more to offer:

  • lightweight threads
  • safe and efficient concurrent data primitives like Mvar and Chan
  • the Big Gun: Software Transactional Memory

I recommend you fork a new thread for every separate point of contact with the outside world, and keep everything coordinated with STM.

like image 115
NovaDenizen Avatar answered Sep 27 '22 19:09

NovaDenizen