0

用例:

  1. 从一台服务器读取数据
  2. 在我的服务器上操作
  3. 将数据发布到其他服务器

但需要每分钟100万的吞吐量。

更多解释:-

假设有 10000 个客户,对于一个客户,我需要调用 5 个 API 并操作数据作为响应,操作后它将创建大约 30 个 API。我想将数据发布到其他服务器。

(假设:为 API 调用获取数据的服务器需要 250 毫秒,而我在其上发布数据的服务器需要 350 毫秒来发布 API 调用的数据。)

伪代码:

In every minute For each customers( there are 10000 customers):


Fetch data from first_server_for_first_service
Fetch data from first_server_for_second_service
Fetch data from first_server_for_third_service
Fetch data from first_server_for_fourth_service
Fetch data from first_server_for_fifth_service

Manipulate data of first_service
Manipulate data of second_service
Manipulate data of third_service
Manipulate data of fourth_service
Manipulate data of fifth_service

post data to second_server_for_first_service_1_type
post data to second_server_for_first_service_2_type
post data to second_server_for_first_service_3_type
post data to second_server_for_first_service_4_type
post data to second_server_for_first_service_5_type
post data to second_server_for_first_service_6_type
post data to second_server_for_second_service_1_type
post data to second_server_for_second_service_2_type
post data to second_server_for_second_service_3_type
post data to second_server_for_second_service_4_type
post data to second_server_for_second_service_5_type
post data to second_server_for_second_service_6_type
post data to second_server_for_third_service_1_type
post data to second_server_for_third_service_2_type
post data to second_server_for_third_service_3_type
post data to second_server_for_third_service_4_type
post data to second_server_for_third_service_5_type
post data to second_server_for_third_service_6_type
post data to second_server_for_fourth_service_1_type
post data to second_server_for_fourth_service_2_type
post data to second_server_for_fourth_service_3_type
post data to second_server_for_fourth_service_4_type
post data to second_server_for_fourth_service_5_type
post data to second_server_for_fourth_service_6_type
post data to second_server_for_fifth_service_1_type
post data to second_server_for_fifth_service_2_type
post data to second_server_for_fifth_service_3_type
post data to second_server_for_fifth_service_4_type
post data to second_server_for_fifth_service_5_type
post data to second_server_for_fifth_service_6_type

我们如何通过 Eventlet 编写代码,以便它可以并行执行这么多任务。还是 eventlet 能够执行这么多任务?

请回复。

4

1 回答 1

4

简短的回答:这是一个硬性要求。如果您绝对不能减少负载,我强烈建议您查看具有内置并发支持的快速语言:Go、Haskell、Ocaml。PyPy 也应该在这种情况下提供帮助。

10000 * 35 = 每分钟 350K API 调用。每秒约 6K。假设响应时间为 350 毫秒,您将需要约 2100 个与上游和下游服务的连接才能跟上。Eventlet 可以毫不费力地托管这么多的 greenthreads。

但是你的CPU有很大的问题。我在旧 Core 2 Duo 盒子上测得的最小 eventlet 开销约为 25µs。每次通话只有 166µs(1 秒/6K 操作)。祝你在 Python 中用 140µs 完成有用的数据处理。好消息是您应该能够在单独的进程中处理每个客户端,例如 1000 个客户端,并将 CPU 负载分散到 10 个内核。

用 Eventlet 解决这个任务不需要任何特别有趣的代码。下面的示例代码可能是最简单的方法。您的 API 调用必须能够重用现有的套接字连接。您可能希望使用队列或信号量添加并发或吞吐量限制。

clients = ['client1', 'client2', ...] # 10K


def service1(request):
    data1 = API.get()
    data2 = process(data1)
    eventlet.spawn(API.post_type_1, data2)
    eventlet.spawn(API.post_type_2, data2)
    # ...


def tick():
    now = time.time()
    for client in clients:
        # some context object
        request = (client, now)

        eventlet.spawn(service1, request)
        eventlet.spawn(service2, request)
        eventlet.spawn(service3, request)
        eventlet.spawn(service4, request)
        eventlet.spawn(service5, request)


def main():
    while True:
        tick()
        eventlet.sleep(60)
于 2014-01-08T11:15:12.287 回答