[Twisted-Python] Crochet 1.5.0: Use Twisted in Django, Flask, or anywhere else
![](https://secure.gravatar.com/avatar/d7875f8cfd8ba9262bfff2bf6f6f9b35.jpg?s=120&d=mm&r=g)
Do you wish you could: * Use the power of Twisted's asynchronous networking in Django, Flask, or other threaded applications? * Provide a blocking API powered by Twisted underneath, without exposing it to the caller? * Write a library that provides APIs both for Twisted and non-Twisted applications? * Use threads more easily inside Twisted applications? Crochet lets you do all that, and more, by hiding and automatically starting the Twisted reactor and providing a blocking API for calling into Twisted. If you need to use your library from a normal Twisted application you can disable the auto-start functionality. Here's an example of using Twisted in a blocking manner: from __future__ import print_function from twisted.names import client from crochet import setup, wait_for setup() @wait_for(timeout=5.0) def gethostbyname(name): """Use the Twisted DNS library.""" 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) New in 1.5.0 is official Python 3.5 support; Python 2.6, 3.3 and older versions of Twisted are no longer officially supported (but are likely to still work). If you need help using Crochet (or just general Twisted or Python help) I am currently available for short-term consulting. You can read the documentation at https://crochet.readthedocs.org/. --Itamar Turner-Trauring
![](https://secure.gravatar.com/avatar/d7875f8cfd8ba9262bfff2bf6f6f9b35.jpg?s=120&d=mm&r=g)
I messed up the email headers on this, so PLEASE BE CAREFUL if you're replying to this, or the reply will end up spamming python-announce-list. Sorry.
![](https://secure.gravatar.com/avatar/d7875f8cfd8ba9262bfff2bf6f6f9b35.jpg?s=120&d=mm&r=g)
On 04/13/2016 06:52 PM, Glyph wrote:
Well, setup() runs the reactor in a thread, which is probably not what you want in a regular Twisted program. For Twisted-native applications interacting with Crochet you don't want to do the auto-reactor mode, so you call no_setup() earlier in startup process, and then subsequent setup() calls (e.g. from other packages) are ignored. So given it would have to have the same API as currently, I'm not sure what the benefit of integrating it into Twisted would be.
![](https://secure.gravatar.com/avatar/e1554622707bedd9202884900430b838.jpg?s=120&d=mm&r=g)
One advantage of having this directly supported in Twisted, then, would be calling no_setup() (or equivalent) early on in 'twistd' and equivalent tools, so that crochet-using modules could be imported as part of a twisted-native plugin?
So given it would have to have the same API as currently, I'm not sure what the benefit of integrating it into Twisted would be.
It might also be able to have a slightly different API. For example, maybe setup() or some analogue thereof could set up an 'atexit' hook that runs the reactor if nobody gets around to calling run() before it exits? -glyph
![](https://secure.gravatar.com/avatar/d7875f8cfd8ba9262bfff2bf6f6f9b35.jpg?s=120&d=mm&r=g)
On 04/13/2016 10:57 PM, Glyph wrote:
Huh. Well... Crochet exits when main thread exits, so if atexit was called that means there's no more threads. So that probably wouldn't work. More broadly, Crochet is a pile of these sort of horrible hacks, encapsulated in one place and hidden as much as possible. But there's rough edges that poke out occasionally. I'm not sure Twisted ought to be that kind of library.
![](https://secure.gravatar.com/avatar/e1554622707bedd9202884900430b838.jpg?s=120&d=mm&r=g)
On Apr 14, 2016, at 5:45 AM, Itamar Turner-Trauring <itamar@itamarst.org> wrote:
Crochet is a pile of these sort of horrible hacks, encapsulated in one place and hidden as much as possible. But there's rough edges that poke out occasionally. I'm not sure Twisted ought to be that kind of library.
We already have an IMAP server so I feel like that ship may have sailed ;-) -g
![](https://secure.gravatar.com/avatar/1327ce755b24b956995d68accae3eab2.jpg?s=120&d=mm&r=g)
On Wed, 13 Apr 2016 at 23:53 Glyph <glyph@twistedmatrix.com> wrote:
+1 on integrating this functionality into Twisted. I wrote https://github.com/testing-cabal/testtools/blob/master/testtools/twistedsupp... before Crotchet came out. It's been used for years as part of testtools with great success. I don't advocate integrating it into Twisted, but I think that whoever integrates Crotchet should at least take a look in case it catches edge cases that Crotchet misses. jml
![](https://secure.gravatar.com/avatar/d7875f8cfd8ba9262bfff2bf6f6f9b35.jpg?s=120&d=mm&r=g)
I messed up the email headers on this, so PLEASE BE CAREFUL if you're replying to this, or the reply will end up spamming python-announce-list. Sorry.
![](https://secure.gravatar.com/avatar/d7875f8cfd8ba9262bfff2bf6f6f9b35.jpg?s=120&d=mm&r=g)
On 04/13/2016 06:52 PM, Glyph wrote:
Well, setup() runs the reactor in a thread, which is probably not what you want in a regular Twisted program. For Twisted-native applications interacting with Crochet you don't want to do the auto-reactor mode, so you call no_setup() earlier in startup process, and then subsequent setup() calls (e.g. from other packages) are ignored. So given it would have to have the same API as currently, I'm not sure what the benefit of integrating it into Twisted would be.
![](https://secure.gravatar.com/avatar/e1554622707bedd9202884900430b838.jpg?s=120&d=mm&r=g)
One advantage of having this directly supported in Twisted, then, would be calling no_setup() (or equivalent) early on in 'twistd' and equivalent tools, so that crochet-using modules could be imported as part of a twisted-native plugin?
So given it would have to have the same API as currently, I'm not sure what the benefit of integrating it into Twisted would be.
It might also be able to have a slightly different API. For example, maybe setup() or some analogue thereof could set up an 'atexit' hook that runs the reactor if nobody gets around to calling run() before it exits? -glyph
![](https://secure.gravatar.com/avatar/d7875f8cfd8ba9262bfff2bf6f6f9b35.jpg?s=120&d=mm&r=g)
On 04/13/2016 10:57 PM, Glyph wrote:
Huh. Well... Crochet exits when main thread exits, so if atexit was called that means there's no more threads. So that probably wouldn't work. More broadly, Crochet is a pile of these sort of horrible hacks, encapsulated in one place and hidden as much as possible. But there's rough edges that poke out occasionally. I'm not sure Twisted ought to be that kind of library.
![](https://secure.gravatar.com/avatar/e1554622707bedd9202884900430b838.jpg?s=120&d=mm&r=g)
On Apr 14, 2016, at 5:45 AM, Itamar Turner-Trauring <itamar@itamarst.org> wrote:
Crochet is a pile of these sort of horrible hacks, encapsulated in one place and hidden as much as possible. But there's rough edges that poke out occasionally. I'm not sure Twisted ought to be that kind of library.
We already have an IMAP server so I feel like that ship may have sailed ;-) -g
![](https://secure.gravatar.com/avatar/1327ce755b24b956995d68accae3eab2.jpg?s=120&d=mm&r=g)
On Wed, 13 Apr 2016 at 23:53 Glyph <glyph@twistedmatrix.com> wrote:
+1 on integrating this functionality into Twisted. I wrote https://github.com/testing-cabal/testtools/blob/master/testtools/twistedsupp... before Crotchet came out. It's been used for years as part of testtools with great success. I don't advocate integrating it into Twisted, but I think that whoever integrates Crotchet should at least take a look in case it catches edge cases that Crotchet misses. jml
participants (3)
-
Glyph
-
Itamar Turner-Trauring
-
Jonathan Lange