Twisted web POST example w/ JSON
This is an example of a simple asynchronous Python web server using Twisted. This is a copy of Jp Calderone's Twisted Web in 60 seconds: handling POSTs example modified to accept a JSON payload in the POST request instead of form data. It also uses his Simple Python Web Server example to run the web server as a daemon with twistd.
webserver.py
"""
http://jcalderone.livejournal.com/49707.html
http://labs.twistedmatrix.com/2008/02/simple-python-web-server.html
usage:
$ twistd -y webserver.py
"""
from pprint import pprint
from twisted.application.internet import TCPServer
from twisted.application.service import Application
from ...Quick notes on trying the Twisted websocket branch example
Here are my quick notes on trying out the websocket example in Twisted's websocket branch. The documentation is here. The Twisted ticket is here. This came about after some conversation with @clemesha on Twitter.
(A Web Socket is a new, still-in-development technology, introduced in HTML5, and may be used for real-time web applications. It provides a simple (maybe better) alternative to existing Comet technology.)
(Note: The WebSocket API is still changing. Google Chrome supports (a version of) it. Firefox as of version 3.6, does not support it yet.)
(I am no expert on Web Sockets. I just think ...
... read more »Running a Twisted Perspective Broker example with twistd
I've been using Twisted's Perspective Broker to manage networking for my Python program. Perspective Broker allows me to run a Python program on a remote machine and perform remote method calls on an object in the Python program. It also allows me to serialize objects and transfer them over TCP.
Once I got a Perspective Broker server and client running, I wanted
to create a "Twisted Application" and run it using twistd,
the
Twisted Daemon. Two major options are:
creating a .tac file and
creating a twistd plugin. Below, I show the steps I took to create
a ...
Twisted links
Twisted tutorials
-
Twisted - hello, asynchronous programming
by Jesse Noller, February 11, 2009
Overview of Twisted concepts -
Twisted inlineCallbacks and deferredGenerator
by Marcin Kasperski, August 13, 2008
Examples comparing raw deferreds, deferredGenerator, and inlineCallbacks. -
Async Batching with Twisted: A Walkthrough
by Duncan McGreggor, June 20, 2008
This article features 8 easy to understand examples demonstrating Deferreds, callbacks, DeferredLists, DeferredSemaphores, and task.Cooperator. -
Concurrency with Python, Twisted, and Flex
by Bruce Eckel, May 3, 2008 -
Grokking Twisted
by Bruce Eckel, April 15, 2006 -
Event-Driven Programming with Twisted and Python
Linux Journal, January 26, 2005 - The Twisted Matrix Framework: Part One, Understanding Asynchronous ...
Can't block for a Deferred in Twisted
Despite the existence of the promising
waitForDeferred/deferredGenerator and the newer
inlineCallbacks, it appears there is no way to block
while waiting for a Deferred. Brian Granger
described the problem on the Twisted mailing list:
I have a function that returns a Deferred. I need to have the result of this Deferred returned in a (apparently) blocking/synchronous manner:glyph provided ... ... read more »def myfuncBlocking(): d = myfuncReturnsDeferred() ... result = return resultI need to be able to call this function like:result = myfuncBlocking()The question is how to get the result out of the Deferred() and make it *look* like myfuncBlocking() has blocked.
Running functions periodically using Twisted's LoopingCall
Twisted is pretty
cool-- it is very powerful, but I haven't had the easiest time
learning it.
Here is a simple example that runs a couple functions
periodically (at different
rates) using LoopingCall.
For more information, here are the Twisted docs for LoopingCall.
from datetime import datetime
from twisted.internet.task import LoopingCall
from twisted.internet import reactor
def hyper_task():
print "I like to run fast", datetime.now()
def tired_task():
print "I want to run slowly", datetime.now()
lc = LoopingCall(hyper_task)
lc.start(0.1)
lc2 = LoopingCall(tired_task)
lc2.start(0.5)
reactor.run()
Results:
I like to run ...... read more »
Notes on parallel processing with Python and Twisted
Twisted is a networking engine written in Python, that among many other things, can be used to do parallel processing. It is very big, though, so I had a hard time finding what I needed. I browsed through the Twisted Documentation and the Twisted O'Reilly book. There is also a Recipe in the Python Cookbook. However, I found Bruce Eckel's article, Concurrency with Python, Twisted, and Flex to be the most helpful. (See also Bruce Eckel's initial article on Twisted: Grokking Twisted)
Here are my notes on running Bruce Eckel's example. I removed the Flex part ...
... read more »About
I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »
Search Blog
Tags
-
algorithms
(5)
-
aws
(9)
-
blogproject
(20)
-
c_cplusplus
(12)
-
cardstore
(8)
-
colinux
(2)
-
concurrency
(13)
-
conkeror
(2)
-
core
(2)
-
cygwin
(17)
-
datastructures
(14)
-
datetime
(4)
-
decorators
(4)
-
django
(40)
-
emacs
(22)
-
files_directories
(11)
-
git
(5)
-
hardware
(5)
-
install_setup
(8)
-
javascript
(3)
-
keyboard
(9)
-
matplotlib
(5)
-
mercurial
(4)
-
nginx
(2)
-
persistence
(5)
-
preferences
(7)
-
processes
(4)
-
pyqt
(18)
-
python
(144)
-
ratpoison
(3)
-
regexes
(6)
-
rsync
(3)
-
softwaretools
(17)
-
sql
(14)
-
ssh
(10)
-
subversion
(6)
-
twisted
(7)
-
ubuntu
(65)
-
urxvt
(5)
-
vxworks
(25)
-
webdev
(5)
-
wmii
(7)
Blogroll
- Adam Gomaa
- Alex Clemesha
- Amir Salihefendic
- Armin Ronacher
- David Beazley
- David Ziegler
- Duncan McGreggor
- Gareth Rushgrave
- Glyph Lefkowitz
- Guido van Rossum
- Ian Bicking
- Jacob Kaplan-Moss
- James Bennett
- James Tauber
- Jesper Noehr
- Marty Alchin
- Matt Harrison
- Nikolay Kolev
- Parand Darugar
- Peter Baumgartner
- Peter Bengtsson
- Rob Hudson
- Simon Willison
- Will McGugan