0

Based on this answer, I was able to create and deploy 2 web services. However, if one service tried to call another, it hanged there until timeout.

My code is:

from wsgiref.simple_server import make_server
from spyne.application import Application
from spyne.protocol.soap import Soap11
from spyne.util.wsgi_wrapper import WsgiMounter
from suds.client import Client
from spyne.decorator import srpc
from spyne.service import ServiceBase
from spyne.model.primitive import Unicode

class Service_Caller(ServiceBase):
  @srpc(_returns=Unicode)
  def call_service():
    client = Client("http://localhost:8000/hello?wsdl")
    result = client.service.say_hello('world')
    return result

class HelloWorldService(ServiceBase):
  @srpc(Unicode, _returns=Unicode)
  def say_hello(name):
      return [u'Hello, %s' % name]

if __name__ == '__main__':
  app1 = Application([Service_Caller], 'example1',
        in_protocol=Soap11(), out_protocol=Soap11())
  app2 = Application([HelloWorldService], 'example2',
        in_protocol=Soap11(), out_protocol=Soap11())
  wsgi_app = WsgiMounter({"caller":app1, "hello":app2})
  server = make_server('0.0.0.0', 8000, wsgi_app)
  server.serve_forever()

Service is called using:

from suds.client import Client
client = Client('http://localhost:8000/caller?wsdl')
client.service.call_service()

At the moment, the only way for my code to work is to deploy 2 services on different domain or on different port. I wonder if anyone has the same problem and knows any workaround. Thanks.

4

1 回答 1

1

这是因为您正在使用的 WSGI 实现 ( wsgiref)。wsgiref只是一个参考 Wsgi 实现,不支持并发。对于生产用途,您应该切换到适当的 wsgi 容器,例如 mod_wsgi、CherryPy、twisted 等,并且永远不要使用wsgiref.

再一次,我不能强调这一点,永远不要用于wsgiref生产用途。

话虽这么说,如果你只想从另一个服务调用一个服务,有一个更好的方法:

from spyne.util.appreg import get_application

app = get_application('example2', 'Application').null
print app.service.say_hello('world')

这是完整的工作示例:

from spyne.application import Application
from spyne.protocol.soap import Soap11
from spyne.util.wsgi_wrapper import WsgiMounter
from spyne.decorator import srpc
from spyne.service import ServiceBase
from spyne.model.primitive import Unicode
from spyne.util.appreg import get_application

class Service_Caller(ServiceBase):
  @srpc(_returns=Unicode)
  def call_service():
    # 'Application' is the default when you omit the `name` argument to the
    # `Application` initializer.
    app1 = get_application('example2', 'Application').null
    result = '\n'.join(app1.service.say_hello('world'))

    return result

class HelloWorldService(ServiceBase):
  @srpc(Unicode, _returns=Unicode)
  def say_hello(name):
      return [u'Hello, %s' % name]

if __name__ == '__main__':
  import logging
  logging.basicConfig(level=logging.DEBUG)
  app1 = Application([Service_Caller], 'example1',
        in_protocol=Soap11(), out_protocol=Soap11())
  app2 = Application([HelloWorldService], 'example2',
        in_protocol=Soap11(), out_protocol=Soap11())
  wsgi_app = WsgiMounter({"caller":app1, "hello":app2})

  from wsgiref.simple_server import make_server
  server = make_server('0.0.0.0', 8000, wsgi_app)
  server.serve_forever()

我希望这会有所帮助。

于 2013-12-01T22:20:13.370 回答