【问题标题】:Making multiple requests to get data from the CherryPy web service发出多个请求以从 CherryPy Web 服务获取数据
【发布时间】:2014-10-30 12:09:53
【问题描述】:

我创建了一个 CherryPy 网络服务,它将数据存储在地图中,从客户端接收密钥并返回相应的数据:

import sys
import imp
import cherrypy

data_source = get_data() # get data from the database and store it in the map

class Provider:
    exposed = True

    def POST(self, key):
        global data_source
        data = data_source[key] # get stored data based on given key
        return data

if __name__ == '__main__':
    cherrypy.tree.mount(Provider(), '/Provider',{'/':
            {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
        })
    cherrypy.config.update({'server.socket_host': '0.0.0.0',
            'server.socket_port': 8080,
        })
    cherrypy.server.max_request_body_size = 1048576000
    cherrypy.engine.start()
    cherrypy.engine.block()

然后,在另一台机器上,我创建了一个脚本来向提供者请求数据。使用该脚本,可以指定我要发出多少并发请求:

import requests
import time
from threading import Thread

def make_request(id, key):
    start = time.time()
    r = requests.post("http://provider-host/Provider", {'key':key})
    end = time.time()
    print 'Thread {0} takes {1} seconds to finish with status code {2}'.format(id, end - start, r.status_code)

def start(num, key):
    ts = []
    for i in range(num):
        t = Thread(target=make_request, args=(i, key))
        ts.append(t)
    for t in ts: t.start()
    for t in ts: t.join()

最后,我做了一个测试,请求同一个密钥 10 次,使用 2 种不同的方法:顺序和并发。

顺序法:

time for i in range(10): start(1, 'big_data_key')

结果是:

Thread 0 takes 2.51558494568 seconds to finish with status code 200
Thread 0 takes 2.47761011124 seconds to finish with status code 200
Thread 0 takes 2.66229009628 seconds to finish with status code 200
Thread 0 takes 2.47381901741 seconds to finish with status code 200
Thread 0 takes 2.4907720089 seconds to finish with status code 200
Thread 0 takes 2.93357181549 seconds to finish with status code 200
Thread 0 takes 2.47671484947 seconds to finish with status code 200
Thread 0 takes 2.40888786316 seconds to finish with status code 200
Thread 0 takes 2.6319899559 seconds to finish with status code 200
Thread 0 takes 2.77075099945 seconds to finish with status code 200
CPU times: user 1.79 s, sys: 1.06 s, total: 2.85 s
Wall time: 25.9 s

并发方法:

time start('138.251.195.251', 10, 'big_data_key')

结果是:

Thread 5 takes 15.5736939907 seconds to finish with status code 200
Thread 1 takes 19.4057281017 seconds to finish with status code 200
Thread 7 takes 21.4743158817 seconds to finish with status code 200
Thread 8 takes 22.4408829212 seconds to finish with status code 200
Thread 0 takes 24.1915988922 seconds to finish with status code 200
Thread 2 takes 24.3175201416 seconds to finish with status code 200
Thread 6 takes 24.3368370533 seconds to finish with status code 200
Thread 4 takes 24.3618791103 seconds to finish with status code 200
Thread 9 takes 24.3891952038 seconds to finish with status code 200
Thread 3 takes 24.5536601543 seconds to finish with status code 200
CPU times: user 2.34 s, sys: 1.67 s, total: 4.01 s
Wall time: 24.6 s

很明显,使用并发方式,完成一个请求所花费的时间比顺序方式要长。

所以,我的问题是:下载时间的差异是仅由两台机器之间的带宽还是由其他原因引起的,例如樱桃相关?如果它是由其他原因引起的,我将不胜感激任何处理它的建议。

【问题讨论】:

  • 告诉我们big_data_keyhuge_text.txt 的大小。以及机器之间网络连接的平均带宽(例如wget一些大文件)。然后,您可以计算串行和并行情况下的预期时序。
  • 嗨,我已经解决了我的问题,以便只使用一个键。数据大小为 22mb,带宽为 10.8 mb/sec。

标签: python networking cherrypy


【解决方案1】:

那么很明显你的瓶颈是网络。以 10.8MiB/s 传输 220MiB 至少需要约 20 秒。您的实验需要约 25 秒,即 8.8MiB/s,即在 100Mbit/s 的最大理论容量中有效约 74Mbit/s。考虑到所有可能的测量误差,这是一个很好的结果。

串行和并行情况之间的差异 (~5%) 表明多路复用没有帮助,因为瓶颈是网络带宽,而不是单个连接的限制。

要衡量 CherryPy 的影响,您可以设置一个用本机代码编写的网络服务器,我建议 nginx,将文件放在那里,然后尝试下载 10 次。对于并行测试,您可以尝试Apache ab,例如ab -n 10 -c 10 http://provider-host/some-big-file

还有一些关于 CherryPy 的注意事项:

  • CherryPy 是一个线程服务器,默认线程池server.thread_pool,有10个worker,
  • 尝试在您的配置中激活 gzip 压缩 '/' : {'tools.gzip.on': True},这将显着提升纯文本数据。

您还可以查看this question,了解如何使用 CherryPy 处理大文件下载。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多