Crochet 1.2.0 - Use Twisted Anywhere!

Itamar Turner-Trauring itamar at
Mon Apr 14 16:31:02 CEST 2014


Crochet is an MIT-licensed library that makes it easier to use Twisted
from regular blocking code. Some use cases include: 

 	* Easily use Twisted from a blocking framework like Django or Flask.
 	* Write a library that provides a blocking API, but uses Twisted for
its implementation.
 	* Port blocking code to Twisted more easily, by keeping a backwards
compatibility layer.
 	* Allow normal Twisted programs that use threads to interact with
Twisted more cleanly from their threaded parts. For example this can be
useful when using Twisted as a WSGI container.

New in this release: 

 	* crochet.wait_for implements the timeout/cancellation pattern
documented in previous versions of Crochet.
 	* Functions wrapped with wait_for and run_in_reactor can now be
accessed via the wrapped_function attribute, to ease unit testing of the
underlying Twisted code.
 	* Bug fixes, documentation improvements and more - for a full list see

Crochet can be downloaded from [1] 

Documentation can be found at [2] 

Bugs and feature requests should be filed at the project [3] 

Here's an example of a program using Crochet. Notice that you get a
completely blocking interface to Twisted and do not need to run the
Twisted reactor, the event loop, yourself. 

Do a DNS lookup using Twisted's APIs.
from __future__ import print_function

# The Twisted code we'll be using:
from twisted.names import client

from crochet import setup, wait_for

# Crochet layer, wrapping Twisted's DNS library in a blocking call.
def gethostbyname(name):
 """Lookup the IP of a given hostname.

 Unlike socket.gethostbyname() which can take an arbitrary amount of
 to finish, this function will raise crochet.TimeoutError if more than 5
 seconds elapse without an answer being received.
 d = client.lookupAddress(name)
 d.addCallback(lambda result: result[0][0].payload.dottedQuad())
 return d

if __name__ == '__main__':
 # Application code using the public API - notice it works in a normal
 # blocking manner, with no event loop visible:
 import sys
 name = sys.argv[1]
 ip = gethostbyname(name)
 print(name, "->", ip)

Run on the command line: 

> $ python
> ->



More information about the Python-announce-list mailing list