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.
""" 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 ...
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.)
(I am no expert on Web Sockets. I just think ...... read more »
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
Twisted Daemon. Two major options are:
creating a .tac file and
creating a twistd plugin. Below, I show the steps I took to create
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
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 ...
Despite the existence of the promising
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.
Twisted is pretty
cool-- it is very powerful, but I haven't had the easiest time
Here is a simple example that runs a couple functions
periodically (at different
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()
I like to run ...... read more »
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 »