Arc Forumnew | comments | leaders | submitlogin
Preforking server in wart
3 points by akkartik 5341 days ago | 2 comments
I found a simple common lisp preforking server (http://tomayko.com/writings/unicorn-is-unix) and gave wart the primitives necessary to make it clean:

  (def start-server()
    (w/socket s 1978
      (repeat 2
        (fork (thunk
                (handle-request s))))
      (wait-for-children)))

  (def handle-request(socket)
    (accepting stream :from socket
      (format stream "Hello ~A, this is PID ~D~%"
              (read-line stream) (sb-posix:getpid))
      (flush stream)))
https://github.com/akkartik/wart/blob/f5fe0903ebce7001ee2efd03f549f4190d389f20/068prefork.wart

To try it out (you'll need SBCL):

  $ git clone https://github.com/akkartik/wart.git
  $ cd wart
  $ git checkout bcad0b2b1752765271987c8fe95f3d47c54c46e2
  $ wart
  wart> (start-server)
In a new tty:

  $ wart
  wart> (client)
  "Hello PID ccc, this is PID sss"
  wart> (client)
  "Hello PID ccc, this is PID ttt" ; different server process
Let me know if you have alternative API suggestions, especially for accepting, my take on the UNIX accept() call.


1 point by akkartik 5341 days ago | link

Corresponding idiom for client:

  (connecting stream :at 1978
    (format stream "PID ~D~%" (getpid))
    (flush stream)
    (read-line stream))
https://github.com/akkartik/wart/blob/bcad0b2b1752765271987c...

I'm using :to for hostname and :at for port number. Does that seem intuitive?

-----

1 point by evanrmurphy 5341 days ago | link

> I'm using :to for hostname and :at for port number. Does that seem intuitive?

Yes, to me it does.

-----