[Twisted-Python] Twisted sure is Twisted :)
![](https://secure.gravatar.com/avatar/08cc709a3da3b07e8f8d96ccce6b385e.jpg?s=120&d=mm&r=g)
Twisted once u get a grip on it sure is pretty impressive. I used twisted to forward UDP packets as a relay. Take one UDP packet and then retrans it to many other clients. On a FreeBSD box P3 750 the delay in retrans on packet to like 5 clients was less then 1ms which was very impressive. In using twisted i know the reactor is broken in wxpython. What would happen if u ran a normal reactor in a seperate thread, and then passed the info from twisted into wxpython using an event handler similar to the wxpython thread demo ?? The only problem with that is i can not see how u would pass info from wxpython back into twisted. Hmmmm maybe if u made a queue, and then passed the commands onto a queue and then the reactor would pull them off. Something to ponder :). Solution on ActiveState cookbook will work, but i do not like timers. The idea behind deferred is intresting. The defered call back does not seem to fire until it is added. How the heck does that work ? I am refering to the database example. Hmmmm not really important but kinda neat. def getAge(user): return dbpool.runQuery("SELECT age FROM users WHERE name = ?", user) def printResult(l): if l: print l[0][0], "years old" else: print "No such user" d=getAge("joe") d.addCallback(printResult) like here d is the function getAge. My gut instict tell me the moment I call getAge the database query should be run. But yet it is not run till u add the callback. and if i add an errorback after it, it is not run till it has been added also, I can not see how it works :)). Hmmmm must be part of the magic :)) Starting to look into woven. Basic examples are impressive. It is a real object oriented web design. In the sandbox on CVS in the directory glyph there is an example on forms. I am unable to get it to work. It runs but eveytime I try and make it work I get in my browser the error no such child resource. I guess I have to keep pluggin away at it :). My limited experience shows i need to change my thinking, since twisted is not a "API" system like PHP, but more of a framework. Maybe it would help if i drank a bottle of vodka first :) I guess the only complaint i have about twisted is lack of woven examples, like for forms and guard. But heya it is opensource and free, so if i do not like it I can use some crappy comercial product :)). Anyways Twisted is really cool. A lot of it seems to be very magical :). Pondering whether I should get rid of my apache server and run a twisted one :)). Anyway this is the crap i ramble off at 4 am :) Just basically wanted to say twisted is cool.
![](https://secure.gravatar.com/avatar/b3407ff6ccd34c6e7c7a9fdcfba67a45.jpg?s=120&d=mm&r=g)
On Sat, Aug 30, 2003 at 04:34:58AM -0600, John Janecek wrote:
You could use reactor.callInThread(myWxMainLoop), I guess -- but last time I looked at wxpython, it *had* to be run in the main thread, so this might get slightly messy (I'm pretty sure the Twisted reactor doesn't mind being run in a non-main thread, so this shouldn't be insurmountable). Of course, it's been quite a while since I've used wxpython, so this might not be an issue anymore.
The only problem with that is i can not see how u would pass info from wxpython back into twisted. Hmmmm maybe if u made a queue,
Just use reactor.callFromThread -- a thread can call this to safely schedule a function to be run in the main event loop. See http://twistedmatrix.com/documents/howto/threading.
Deferred callbacks obviously can't fire until they're added to the deferred, but of course they might not fire immediately upon addCallback, though -- because the deferred might not have been called yet.
No, here 'd' is a deferred result from the function 'getAge'. A deferred is simply an object that represents a result that probably hasn't arrived yet.
So, in this case, dbpool.runQuery ducks off and runs the database query in a thread behind the scenes, and returns immediately without waiting for the result to arrive -- because databases can take an arbitrarily long time to return a result. In the meantime, the code that called runQuery can add callbacks and errbacks to the deferred result from runQuery, and the deferred will see to it that those are run when the result does arrive (if the result has already arrived by the time you add a callback, it will be run immediately). The running of the callback is completely independent to the running of the query. If you never add any callbacks or errbacks and just discard the deferred from dbpool.runQuery immediately, the query is still run on the database -- it's just that when the twisted.enterprise.adbapi code gets the result back from the database, it will fire the deferred (using Deferred.callback or Deferred.errback), and the deferred will find it has no callbacks to trigger, which is fine. If you haven't already, read http://twistedmatrix.com/documents/howto/defer. [...]
Anyway this is the crap i ramble off at 4 am :) Just basically wanted to say twisted is cool.
Thanks! :) -Andrew.
![](https://secure.gravatar.com/avatar/b3407ff6ccd34c6e7c7a9fdcfba67a45.jpg?s=120&d=mm&r=g)
On Sat, Aug 30, 2003 at 04:34:58AM -0600, John Janecek wrote:
You could use reactor.callInThread(myWxMainLoop), I guess -- but last time I looked at wxpython, it *had* to be run in the main thread, so this might get slightly messy (I'm pretty sure the Twisted reactor doesn't mind being run in a non-main thread, so this shouldn't be insurmountable). Of course, it's been quite a while since I've used wxpython, so this might not be an issue anymore.
The only problem with that is i can not see how u would pass info from wxpython back into twisted. Hmmmm maybe if u made a queue,
Just use reactor.callFromThread -- a thread can call this to safely schedule a function to be run in the main event loop. See http://twistedmatrix.com/documents/howto/threading.
Deferred callbacks obviously can't fire until they're added to the deferred, but of course they might not fire immediately upon addCallback, though -- because the deferred might not have been called yet.
No, here 'd' is a deferred result from the function 'getAge'. A deferred is simply an object that represents a result that probably hasn't arrived yet.
So, in this case, dbpool.runQuery ducks off and runs the database query in a thread behind the scenes, and returns immediately without waiting for the result to arrive -- because databases can take an arbitrarily long time to return a result. In the meantime, the code that called runQuery can add callbacks and errbacks to the deferred result from runQuery, and the deferred will see to it that those are run when the result does arrive (if the result has already arrived by the time you add a callback, it will be run immediately). The running of the callback is completely independent to the running of the query. If you never add any callbacks or errbacks and just discard the deferred from dbpool.runQuery immediately, the query is still run on the database -- it's just that when the twisted.enterprise.adbapi code gets the result back from the database, it will fire the deferred (using Deferred.callback or Deferred.errback), and the deferred will find it has no callbacks to trigger, which is fine. If you haven't already, read http://twistedmatrix.com/documents/howto/defer. [...]
Anyway this is the crap i ramble off at 4 am :) Just basically wanted to say twisted is cool.
Thanks! :) -Andrew.
participants (2)
-
Andrew Bennetts
-
John Janecek