Tcl HomeTcl Home Hosted by

Google SiteSearch

This page has a collection of useful Tech Notes that have been contributed by users. Q1 Q2

Tech Notes Index

  • 1. How to shutdown embedded TclHttpd
  • 2. Postgres integration with TclHttpd
  • Questions and Answers

    1. How to shutdown embedded TclHttpd
    [John Buckman of Lyris Technology writes:]

    Here is our scenario: we link Tcl into our multithreaded application, and one thread launches Tcl and runs TclHttpd. This works great, but when our application is told to exit, how do we tell TclHttpd to exit in a thread safe manner? After all, we have TclHttpd in a "vwait forever" loop, from which it never wants to exit

    The solution that we adopted was to create a async event handler beforehand to signal program exit, use a separate thread to notify the async event when it is time to shutdown, and then Tcl runs our C code at a safe time from inside its own thread, which modifies the global "forever" variable, thus causing TclHttpd to gracefully exit. This works nicely, with no memory leaks, and no need to wait for someone to cause an event.

    We looked at the Tcl source code for the asynchronous operations, and all of it is semaphore protected and thread safe (beautiful, clean source code, by the way) so I assume that this is the proper way to do it since it appears from looking at the Tcl code that it was made for this sort of thing.

    2. Postgres integration with TclHttpd
    [Colin McCormak writes:]

    I've got a postgresql tcl interface which doesn't block on the db server backend. Postgresql uses a socket to communicate with the backend. I use Tcl_MakeTcpClientChannel to let Tcl supervise it, set the appropriate options, and expose the postgresql C library functions needed to tcl (using Swig.)

    That way you can just treat it as a normal socket, as long as you let Postgresql's library do the reading.

    I've put up a copy here:

    I've requested a sourceforge project libtclpq

    I'll move it into cvs etc when the project's approved. Some random points:

    1. it works with tcl8.4
    2. it works with the latest postgresql
    3. there's a tcl support suite (and a test file :)
    4. we're using it pretty heavily at $work
    5. it integrates with tclhttpd (although I've never integrated the asynchronous behavior)
    6. there's also a set of itcl stuff (I'll release later) which gives the ability to create widgets from tables, creates browsers, etc.