pythonmultithreadingweb-servicespython-multithreadingspyne

Does Spyne (python web service framework) support multiple threads? If not, is there a way to do using the Python Thread Library?


Due to interoperability, I need to deploy an application as a web service. I'm using Spyne (http://spyne.io/) to do that, a python framework for ws. So far so good.

However, the service will receive several requests at once. Hence, I need to increase perfomance, since the request does several I/O (database, file) tasks.

The following code exemplifie a basic web service in Spyne. Based on that, does Spyne support a thread pool or threading? How can I activate or wrap the service in multiple threads? If not possible, how can I achieve that with Python threading library?

Thanks in advance.

from spyne import Application, rpc, ServiceBase, Integer
from spyne.protocol.soap import Soap11
from spyne.server.wsgi import WsgiApplication

class HelloWorldService(ServiceBase):
    @rpc(Integer, Integer, _returns=Integer)
    def multiply(ctx, a, b):
        return a * b

application = Application([HelloWorldService],
    tns='spyne.multiply',
    in_protocol=Soap11(validator='lxml'),
    out_protocol=Soap11()
)

if __name__ == '__main__':
    # You can use any Wsgi server. Here, we chose
    # Python's built-in wsgi server but you're not
    # supposed to use it in production.
    from wsgiref.simple_server import make_server
    wsgi_app = WsgiApplication(application)
    server = make_server('0.0.0.0', 8000, wsgi_app)
    server.serve_forever()

Solution

  • Spyne works with both blocking/async code in single-threaded or multithreaded settings.

    If you need to do concurrency with multiple threads, use a WSGI server capable of handling multiple threads, like CherryPy, Twisted, mod_wsgi, etc. The WSGI reference implementation that you use in the code sample (wsgiref) does not support concurrency.

    If you need to do concurrency with async method calls, use Twisted.

    Examples are located at https://github.com/arskom/spyne/tree/master/examples