Rusty Brooks wrote:

Actually I can already think of a lot more uses of this. I've been using AOLServer since back in the day when you could share file handles between threads. This made logging much simpler because you could initialize logs on server startup and write to them any time. When it moved to multiple interps that did not share file handles, we had to start doing stuff like queueing file writes to nsvs and flushing those periodically, or opening a file EVERY TIME you wanted to write to it, and wrapping that in mutexes. Either way, kind of a pain... This might enable certain things like that again.

If nsproxy solves your problem, great. If not, here's a different thought - if I was doing this in a vanilla threaded tclsh with the standard thread package, I would simply create one worker thread to handle the logging task and just send that thread messages with thread::send. AOLserver doesn't have that interface, but you can put together something similar fairly simply. It is similar to what you were doing with queuing the file writes to nsvs, but rather than doing a periodic flush explicitly, have a separate thread that waits on a condition to be notified that there is something new to do and immediately wakes up to process that message.

Generically:

proc startworker {name} {
        nsv_set $name mutex [ns_mutex create]
        nsv_set $name cond  [ns_cond create]
        nsv_set $name queue {}
        nsv_set $name tid [ns_thread begindetached [list workermain $name]]
}

proc workermain {name} {
        while(1) {
                ns_cond wait [nsv_get $name cond] [nsv_get $name mutex]
                set queue [nsv_get $name queue]
                nsv_set $name queue [lrange $queue 1 end]
                set msg [lindex $queue 0]
                eval $msg
                # or whatever else you want to do with these messages
        }
}

prod sendworker {name msg} {
        nsv_lappend $name $msg
        ns_conn broadcast [nsv_get $name $cond]
}



startworker logger
sendworker logger {set logfh [open logfile w]}

sendworker logger {puts $logfh msg1}
sendworker logget {puts $logfh msg2}

etc.

-J


--
AOLserver - http://www.aolserver.com/

To Remove yourself from this list, simply send an email to <[EMAIL PROTECTED]> 
with the
body of "SIGNOFF AOLSERVER" in the email message. You can leave the Subject: 
field of your email blank.

Reply via email to