Geoffrey Talvola wrote:
> But for a typical database-driven web application, Webware is a much
> better fit in my opinion because of its threaded architecture.  I have
> some servlets that do as many as a dozen relatively time-consuming SQL
> queries.  If I were using Twisted, because of its single-threaded,
> asynchronous model I'd have to unnaturally break up my code into many
> different pieces to return control to the event loop during each of these
> queries.  Otherwise the appserver would be frozen while these queries were
> running.

  Greetings, this is my first post to the webware list.  A (very) brief
introduction I'm an active Twisted developer (both on and with), and the
maintainer of Twisted.news and Twisted.names.  I hope no one takes offense
at my interjection, I don't mean to bring Twisted advocacy to the Webware
list, just to correct a factual error.

  Twisted does attempt to be single-threaded in any case where this is
possible.  Unfortunately, it isn't -always- possible, usually when using an
extension module that presents a blocking interface.  To deal with this,
Twisted does make it possible to employ threads, in a manner consistent with
the rest of the single-threaded, asynchronous API.  The basic unit of
"callback abstraction" in Twisted is a Deferred object, and these can be
used with threads:

    from twisted.internet import threads
    deferred = threads.deferToThread(longRunningFunction, ...)
    deferred.addCallbacks(handleResult, handleErrors)

  handleResult will be called with whatever object longRunningFunction
chooses to return, or handleErrors will be called if it raises an
exceptions.  Both functions will be invoked in the main thread.

  One doesn't see this very often in the Twisted code, or applications that
use Twisted, because we try so hard to avoid thread usage.

  Jp

-- 
 up 16 days, 4:29, 5 users, load average: 0.30, 0.12, 0.04

Attachment: pgp00000.pgp
Description: PGP signature

Reply via email to