Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试使用一个服务调用部署在同一域中的另一个服务时超时_Python_Web Services_Soap_Suds_Spyne - Fatal编程技术网

Python 尝试使用一个服务调用部署在同一域中的另一个服务时超时

Python 尝试使用一个服务调用部署在同一域中的另一个服务时超时,python,web-services,soap,suds,spyne,Python,Web Services,Soap,Suds,Spyne,基于此,我能够创建和部署2个web服务。然而,如果一个服务试图调用另一个,它将挂起直到超时 我的代码是: 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 impor

基于此,我能够创建和部署2个web服务。然而,如果一个服务试图调用另一个,它将挂起直到超时

我的代码是:

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()
使用以下命令调用服务:

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

目前,我的代码工作的唯一方法是在不同的域或不同的端口上部署2个服务。我想知道是否有人有同样的问题,并知道任何解决办法。谢谢。

这是因为您正在使用的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()

我希望这能有所帮助。

你好,Burak,谢谢你的回答。它起作用了!我决定在我的应用程序中使用cherrypy。