Network programming (select(2)) for Haskell?

Hi, Recently I've written a Perl program that "bridges" 2 rsync commands together. It basically allows remote-to-remote rsyncs from a "control" server . The code is here: http://hpaste.org/fastcgi/hpaste.fcgi/view?id=29852#a29852 In order to get better at network programming using haskell, I'd like to try and port it to Haskell, but I can't seem to locate the equivalent to select(2) in Haskell. Perhaps a different idiom is to be used? These kinds of scripts a pretty much the bread and butter of my daily work, and understanding network programming of this type in Haskell would really help me integrate Haskell into my workplace. Can anyone offer pointers for me to get started? Thanks a lot, Patrick -- ===================== Patrick LeBoutillier Rosemère, Québec, Canada

Hi Patrick, On Fri, Sep 10, 2010 at 2:02 PM, Patrick LeBoutillier < patrick.leboutillier@gmail.com> wrote:
In order to get better at network programming using haskell, I'd like to try and port it to Haskell, but I can't seem to locate the equivalent to select(2) in Haskell. Perhaps a different idiom is to be used?
GHC uses select/epoll/kqueue internally to multiplex its lightweight threads. When you would use a select loop for handle many connections in one language you would typically launch one thread per connection in Haskell, using forkIO. With the upcoming version GHC can handle 100k+ threads this way. Right now the limit is 1024 simultaneous connections due a hard coded limit in select. You code would look something like this: acceptConnections serverSock = do sock <- accept severSock forkIO $ handleConnection sock acceptConnections serverSock handleConnection sock = do msg <- recv send ... sClose sock I'd recommend using the network-bytestring library which provides more efficient versions of e.g. recv and send. Cheers, Johan

Hi Patrick, On 10/09/2010 14:02, Patrick LeBoutillier wrote:
In order to get better at network programming using haskell, I'd like to try and port it to Haskell, but I can't seem to locate the equivalent to select(2) in Haskell. Perhaps a different idiom is to be used?
No select(2) syscall is explicitly available at API level. This is because of how the Haskell (GHC) Runtime is structured. Both the default single threaded runtime that the multi-threaded runtime are capable of hosting different haskell light-threads. IO between lightweight threads is multiplex transparently by the runtime via select(2) o epoll/kqueue in the upcoming ghc. Haskell lightweight threads are very cheap and efficient so the idiomatic way to handle your use case is to spawn different threads with forkIO (you are forking lightweight threads not OS thread) and you some Haskell concurrency primitive for thread communication/synchronization (see MVar and Chan in Control.Concurrent). Paolo
participants (3)
-
Johan Tibell
-
Paolo Losi
-
Patrick LeBoutillier