【问题标题】:Slow webservice problem网络服务慢的问题
【发布时间】:2011-06-02 10:20:56
【问题描述】:

我在 Linux 机器(ubuntu)上创建了一个 Python 网络服务:

import soaplib
import os

from soaplib.core.service import rpc, DefinitionBase, soap
from soaplib.core.model.primitive import String, Integer
from soaplib.core.server import wsgi
from soaplib.core.model.clazz import Array

def runcmd(cmd):
    fout = os.popen(cmd)
    out = fout.read()
    return out

class LinuxServices(DefinitionBase):
@soap(String, String,_returns=Array(String))
def df(self,server, user):
    L = []
    cmd = 'df -hP | grep "/"'
    output = runcmd(cmd).split('\n')
    for n in xrange(len(output)-1):
        out = output[n].split()
        L.append('%s;%s' % (out[5], out[4]))
    return L

if __name__=='__main__':
try:
    from wsgiref.simple_server import make_server
    soap_application = soaplib.core.Application([LinuxServices], 'tns')
    wsgi_application = wsgi.Application(soap_application)
    server = make_server('0.0.0.0', 7789, wsgi_application)
    server.serve_forever()
except ImportError:
    print "Error: example server code requires Python >= 2.5"

我是根据这个例子创建的:soaplib helloworld

然后(在 Windows 7 上)我创建了一个 Silverlight 项目,我使用这个 ws 在我的 linux 服务器上获取磁盘状态:

Silverlight 项目中的服务:

public class LinuxService
{
    [OperationContract]
    public List<dfItem> df()
    {
        List<dfItem> dfItems = new List<dfItem>();

        WebReference.Application app = new WebReference.Application();

        var result = app.df(new WebReference.df()/*...*/);

        foreach (var item in result.dfResult)
        {
            string[] info = item.Split(';');

            dfItem dfItem = new dfItem()
            {
                MountPoint = info[0].ToString(),
                Usage = info[1].ToString()
            };
            dfItems.Add(dfItem);
        }
        return dfItems;
    }
    //...
}

页面调用服务:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    LinuxServiceClient client = new LinuxServiceClient();

    client.dfCompleted += new EventHandler<dfCompletedEventArgs>(client_dfCompleted);

    client.dfAsync();
}

void client_dfCompleted(object sender, dfCompletedEventArgs e)
{
    DG.ItemsSource = e.Result;
    DG.Visibility = System.Windows.Visibility.Visible;
}

我的问题是当我导航到这个页面时,从ws(局域网中的ws)获取数据需要4-8秒。

我真的怀疑线路带宽可以创造这个等待时间......

我的问题: 您有什么建议可以加快速度吗?

系统信息:

  • UbuntuServer 11.04

  • Python:Python 2.7

  • Soaplib:soaplib 2.0.0-beta2


  • Windows:Windows 7 sp1

  • Silverlight:Silverlight 4

【问题讨论】:

  • 如果您告诉我们您正在使用哪些 Python 和 soaplib 版本会有所帮助,因为这不是有效的 2.7/0.8.1。编写一个简单的 Python 肥皂客户端来查看是 Silverlight 还是导致延迟的 Python 也会很有用(我怀疑 Silverlight(因为我总是怀疑 Silverlight))。
  • 添加了有关 python 的信息,...我会尝试编写一个简单的 Python 肥皂客户端..
  • 我测试了 ws 就像在 helloworld 示例中一样...首先运行缓慢,然后运行快速...有什么想法吗?

标签: python windows linux web-services silverlight-4.0


【解决方案1】:

我建议使用 wireshark http://www.wireshark.org/ 通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。

当您开始捕获时,数据量可能看起来很庞大,但如果您能发现任何看起来像您的 SOAP 消息的片段(应该很容易发现),那么您可以快速过滤到正确的对话 -点击并选择“Follow TCP Stream”。

然后,您可以在弹出窗口中看到您编写的 SOAP 服务和 silverlight 客户端之间的整个对话。

如果一切正常,请关闭弹出窗口。作为一个额外的好处,wireshark 将过滤掉片段列表,只显示那些在对话中带有时间戳的片段,以了解它们何时发生。使用它来确定是客户端还是服务器响应缓慢。

如果看起来没有真正的延迟,那么我建议在要求 Silverlight 进行 SOAP 调用和它实际进行网络调用之间存在相当大的延迟。

【讨论】:

  • 我会试试这个,让你知道我发现了什么。谢谢!
  • 我用wireshark对其进行了测试......似乎Silverlight中的问题要求进行SOAP调用......现在我终于知道问题出在哪里了:)
【解决方案2】:

使用suds 客户端和soaplib hello world 示例只是一个粗略的基准测试:

>>> def bench_soap(num):
...:     start = time.time()
...:     for i in range(num):
...:         hello_client.service.say_hello("Dave", 5)
...:     elapsed = time.time() - start
...:     print "Completed %s: ops in %.2f seconds : %.1f ops/sec" % (num, elapsed, num / elapsed)
...:     
...:     

>>> bench_soap(100)
Completed 100: ops in 0.40 seconds : 247.5 ops/sec

>>> bench_soap(1000)
Completed 1000: ops in 3.81 seconds : 262.5 ops/sec

我没有看到任何“滞后”或类似的情况。 Soaplib,似乎快速且响应迅速,所以也许是 Silverlight 问题?还是两者之间存在某种不兼容?

【讨论】:

  • 我多次尝试调用这个服务,但奇怪的是这个响应慢不是恒定的......
猜你喜欢
  • 2012-12-20
  • 2019-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-14
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多