慢的webservice问题

时间:2011-06-02 10:20:56

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

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

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

2 个答案:

答案 0 :(得分:3)

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

当你开始捕获时,数据量可能看起来很大,但是如果你能发现看起来像你的SOAP消息的任何片段(应该很容易发现),那么你可以通过正确的方式迅速过滤到那个对话 - 单击并选择“关注TCP流”。

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

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

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

答案 1 :(得分:1)

使用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问题?或者两者之间有某种不相容?

相关问题