Twisted code ↔ other code
Python, some Twisted probably helps
An object that reacts to events
IReactor{}
Time
: callLater
…
Process
: spawnProcess
Threads
: call(In|From)Thread
, …
(TCP|UDP|SSL|Multicast)
(UNIX|UNIXDatagram|Socket)
FDSet
: (add|remove)(Reader|Writer)
, …
An object you get now,
gets you result or failure later
try: result = blocking_read() except SomeError as e: on_failure(e) else: on_result(result)
d = async_read()
d.addCallbacks(on_result, on_failure)
try: result = yield async_read() except SomeError as e: on_failure(e) else: on_result(result)
Service Oriented Architecture
Web Server Gateway Interface
twistd web --wsgi=wsgi.app
t.w.wsgi
txsockjs
def _getDataAtURL(url): return requests.get(url) # BLOCKS!
def _compute(n): x = 2 for _ in xrange(n): # BLOCKS! x *= x send_somewhere(x)
Alternatives:
IO bound? Asynchronous IO!
CPU bound? Cooperate!
treq
: requests
-like, but asynchronous
def _getDataAtURL(url): return treq.get(url)
t.internet.task.coiterate
& friends
def _compute(n): x = 2 for _ in xrange(n): x *= x yield # Yields to the reactor :) send_somewhere(x) coiterate(_compute(n))
Avoiding blocking isn’t always possible
DBAPI2
, WSGI
…
scrypt
, Pillow
…
Can’t block reactor thread
→ block a different one!
deferToThread
spawnProcess
…
itamarst/crochet
setup()
@run_in_reactor
EventualResult
EventualResult
?Deferred
wait(timeout=None)
from twisted.web.client import getPage from crochet import setup, run_in_reactor setup() @run_in_reactor def download_page(url): return getPage(url) url = "http://tm.tl/5000" result = download_page(url) print result.wait()
class ExchangeRate(object): # ... @run_in_reactor def start(self): self._lc = LoopingCall(self._download) self._lc.start(30, now=True) def _download(self): d = getPage(url) # ...
@app.route('/') def index(): rate = EURUSD.latest_value() if rate is None: rate = "unavailable" return "EUR/USD rate: {0}.".format(rate) app.run()
jyio/geventreactor
install()
gevent-style automagic suspending
r = waitForDeferred(d)
d = waitForGreenlet(g)
TODO
If you want to use Twisted, you probably can.
That doesn’t necessarily mean it’s a good idea.
Although it obviously is ;-)