From: Bruce Butterfield
Subject: Web services in CL
Date: 
Message-ID: <1114301101.897338.125800@o13g2000cwo.googlegroups.com>
I am writing some web services in CL (SBCL for now) and I would like
some advice about how to handle requests. In Java I would use a simple
TCP listener on a known port and spawn a new thread to handle each
request. Threads are at least fairly well documented in Java and I have
had no issues (other than performance) using this model in previous
implementations. Is there an equivalent simple, straightforward way to
spawn new threads of execution upon accepting a TCP connection? I guess
what I'm looking for is 'trivial-threads' to match up with
'trivial-sockets'. Anything out there?

From: Peter Scott
Subject: Re: Web services in CL
Date: 
Message-ID: <1114364419.038071.312150@g14g2000cwa.googlegroups.com>
The kmrcl library has some fairly simple facilities for writing
threaded servers. They work on SBCL and some others, and they handle
some of the nasty details very nicely.

-Peter
From: Karl A. Krueger
Subject: Re: Web services in CL
Date: 
Message-ID: <d4gpac$n4p$1@baldur.whoi.edu>
Bruce Butterfield <···@entricom.com> wrote:
> I am writing some web services in CL (SBCL for now) and I would like
> some advice about how to handle requests.

The expression "Web services" seems to mean a lot of different things
these days.  If you're handling HTTP requests, you might look into TBNL,
a Web toolkit that hides all the threading and socketing and so forth,
letting you deal with sensible things like requests and sessions.

-- 
Karl A. Krueger <········@example.edu> { s/example/whoi/ }
From: Rob Warnock
Subject: Re: Web services in CL
Date: 
Message-ID: <HpudnawvR9eMxPHfRVn-2g@speakeasy.net>
Bruce Butterfield <···@entricom.com> wrote:
+---------------
| I am writing some web services in CL (SBCL for now) and I would like
| some advice about how to handle requests. In Java I would use a simple
| TCP listener on a known port and spawn a new thread to handle each
| request. Threads are at least fairly well documented in Java and I have
| had no issues (other than performance) using this model in previous
| implementations. Is there an equivalent simple, straightforward way to
| spawn new threads of execution upon accepting a TCP connection? I guess
| what I'm looking for is 'trivial-threads' to match up with
| 'trivial-sockets'. Anything out there?
+---------------

I do web programming with CMUCL [with a mod_lisp-like model],
spawning a new CMUCL (green) thread for each request. It seems
to work quite well for the level of traffic those applications
see [somewhat low, to be sure]. I don't know how much of CMUCL's
threading (the "MP" package) and TCP-handling (in the "EXT" package)
was carried over directly to SBCL, but I suspect there are probably
direct equivalents to the functions I use. Look for functions
in SBCL with names or functionality similar to these from CMUCL
to create a "listener" on a Unix-domain socket:

    mp:make-process
    ext:create-unix-listener
    mp:process-wait-until-fd-usable

and to accept a connection and spawn a thread to handle a request:

    ext:accept-unix-connection
    sys:make-fd-stream
    mp:make-process
    unix:unix-close


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Marco Baringer
Subject: Re: Web services in CL
Date: 
Message-ID: <m2d5sjoot8.fsf@soma.local>
"Bruce Butterfield" <···@entricom.com> writes:

> I am writing some web services in CL (SBCL for now) and I would like
> some advice about how to handle requests. In Java I would use a simple
> TCP listener on a known port and spawn a new thread to handle each
> request. Threads are at least fairly well documented in Java and I have
> had no issues (other than performance) using this model in previous
> implementations. Is there an equivalent simple, straightforward way to
> spawn new threads of execution upon accepting a TCP connection? I guess
> what I'm looking for is 'trivial-threads' to match up with
> 'trivial-sockets'. Anything out there?

SLIME contains a small portable threading compatability layer. (with
trivial mailbox based message passing functions). If you're not
willing you add the dependency on slime you could just cut 'n paste
the code (its about 20 lines per implementation).

-- 
-Marco
Ring the bells that still can ring.
Forget the perfect offering.
There is a crack in everything.
That's how the light gets in.
	-Leonard Cohen
From: Lars Rune Nøstdal
Subject: Re: Web services in CL
Date: 
Message-ID: <pan.2005.04.25.07.52.28.623024@gmail.com>
On Sat, 23 Apr 2005 17:05:01 -0700, Bruce Butterfield wrote:

> I am writing some web services in CL (SBCL for now) and I would like some
> advice about how to handle requests. In Java I would use a simple TCP
> listener on a known port and spawn a new thread to handle each request.
> Threads are at least fairly well documented in Java and I have had no
> issues (other than performance) using this model in previous
> implementations. Is there an equivalent simple, straightforward way to
> spawn new threads of execution upon accepting a TCP connection? I guess
> what I'm looking for is 'trivial-threads' to match up with
> 'trivial-sockets'. Anything out there?

i don't know, but maybe something like this:


(defmacro with-thread (&rest body)
  `(sb-thread:make-thread (lambda () ,@body)))

(defun destroy-thread (th)
  (sb-thread:destroy-thread th))


(defun round-trip (socket)
  (format t "Got connection on ~A.~%" socket) (finish-output)
  (sleep 2)
  (format t "Finishing off connection on ~A.~%" socket) (finish-output))
  

(let ((running nil)
      (th nil))
  (defun server ()
    (cond 
      (running
       (destroy-thread th)
       (setf running nil))
      (t
       (format t "call this function again to stop server-main-thread~%")
       (finish-output)
       (setf running t
             th (with-thread
                    (loop
                     ;; wait for and accept incomming connection
                     (sleep 3)
                     (with-thread (round-trip (random 1000))))))))))


note that the two sleeps seem to 'disturb' one another

-- 
mvh,
Lars Rune Nøstdal
http://lars.nostdal.org/
From: Lars Rune Nøstdal
Subject: Re: Web services in CL
Date: 
Message-ID: <pan.2005.04.25.07.54.57.795102@gmail.com>
On Mon, 25 Apr 2005 09:52:29 +0200, Lars Rune Nøstdal wrote:

> On Sat, 23 Apr 2005 17:05:01 -0700, Bruce Butterfield wrote:
> 
>> I am writing some web services in CL (SBCL for now) and I would like
>> some advice about how to handle requests. In Java I would use a simple
>> TCP listener on a known port and spawn a new thread to handle each
>> request. Threads are at least fairly well documented in Java and I have
>> had no issues (other than performance) using this model in previous
>> implementations. Is there an equivalent simple, straightforward way to
>> spawn new threads of execution upon accepting a TCP connection? I guess
>> what I'm looking for is 'trivial-threads' to match up with
>> 'trivial-sockets'. Anything out there?
> 
> i don't know, but maybe something like this:
> 
> 
> (defmacro with-thread (&rest body)
>   `(sb-thread:make-thread (lambda () ,@body)))
> 
> (defun destroy-thread (th)
>   (sb-thread:destroy-thread th))
> 
> 
> (defun round-trip (socket)
>   (format t "Got connection on ~A.~%" socket) (finish-output) (sleep 2)
>   (format t "Finishing off connection on ~A.~%" socket) (finish-output))
>   
>   
> (let ((running nil)
>       (th nil))
>   (defun server ()
>     (cond
>       (running
>        (destroy-thread th)
>        (setf running nil))
>       (t
>        (format t "call this function again to stop server-main-thread~%")
>        (finish-output)
>        (setf running t
>              th (with-thread
>                     (loop
>                      ;; wait for and accept incomming connection (sleep 3)
>                      (with-thread (round-trip (random 1000))))))))))
> 
> 
> note that the two sleeps seem to 'disturb' one another

err .. some of the lines got combined or something when posting ..
the sleep should not be on the commented out line for instance

-- 
mvh,
Lars Rune Nøstdal
http://lars.nostdal.org/
From: Bruce Butterfield
Subject: Re: Web services in CL
Date: 
Message-ID: <1114444463.226827.210700@z14g2000cwz.googlegroups.com>
Lars Rune Nøstdal wrote:
[snip]
Thanks for the code and library suggestions -- it does bring up an
issue I would like some opinions on, however. "Green" or user-space
threads seem like a reasonable option for this useage since the app
itself will be very lightweight and I have no need for the OS to handle
preemption or distribution among multi-processors, etc. SBCL appears to
only support native threads but it occurs to me that it should be
possible to write a portable user-space thread package along the lines
of the various Scheme implementations using continuations. Does
something like this exist out there or are there some
implementation-specific limitations to providing this?
From: Lars Rune Nøstdal
Subject: Re: Web services in CL
Date: 
Message-ID: <pan.2005.04.25.20.36.33.109448@gmail.com>
On Mon, 25 Apr 2005 08:54:23 -0700, Bruce Butterfield wrote:

> "Green" or user-space threads seem like a reasonable option for this
> useage since the app itself will be very lightweight and I have no need
> for the OS to handle preemption or distribution among multi-processors,
> etc. 

> Does something like this exist out there or are there
> some implementation-specific limitations to providing this?

i think the book "On Lisp" talks about this in some of the later
chapters.. you can download it here:
http://www.paulgraham.com/onlisp.html

-- 
mvh,
Lars Rune Nøstdal
http://lars.nostdal.org/