0

I have a somewhat rich python application and do not want to restructure it just for the sake of exposing some http end-points. I simply want to expose them via a set of threads that listen via http on those ports.

There are a couple of questions that are pertinent: but they are quite old and are not exactly what I am looking for:

What would be a "modern" approach and that would involve simply kicking off threads - or even better entries from a threadpool - for incorporating twisted into the existing application?

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560

1 Answers1

1

See https://github.com/itamarst/crochet

Here's an example from the documentation:

#!/usr/bin/python
"""
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
setup()


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

    Unlike socket.gethostbyname() which can take an arbitrary amount of time
    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)
Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122