gpt4 book ai didi

python - Python asyncio Streams 是否维护多个作者和读者的顺序?

转载 作者:行者123 更新时间:2023-12-04 08:52:47 27 4
gpt4 key购买 nike

系统如 beanstalk客户端-服务器通信或 HTTP pipelining通过共享连接保留多个请求和响应之间的顺序。
换句话说,服务器收到的第一个请求是客户端收到的第一个请求
回复为。
使用 Python 时 Streams作为客户端访问这种系统,Python 是否同样维护作者和读者之间的顺序?在这种情况下,我们有:

  • 打开一个连接,得到一个 StreamWriter和一个 StreamReader实例
  • 在 asyncio 应用程序中共享那些(在同一个 asyncio 事件循环线程中)
  • 异步应用程序的不同部分写入和读取(等待)它们

  • 访问流的代码将位于单个位置,例如在下面的示例中,但可以在收到任何响应之前从应用程序的不同部分重复使用。
    async def request(host, port, data):
    reader, writer = await asyncio.open_connection(host, port)
    writer.write(data)
    response = await reader()
    ...
    所以当我们的应用程序调用了 request() & 写入流,传输数据,远程服务器工作,最后返回响应。
    但在那之前,我们等待响应,其他代码运行,调用我们的 request()函数发送更多请求并依次等待响应。
    那么,在这里用更实际的术语来表达这个问题,许多读者等待者是否总是按照他们发送请求的相同顺序访问响应?
    有点类似的问题,但没有为这个问题提供答案:
    Does Python's asyncio lock.acquire maintain order?

    最佳答案

    答案是:没有。
    据我所知,没有保证处理顺序的有序队列。
    一个简单的 PoC 是要点 https://gist.github.com/HQJaTu/345f7147065f1e10587169dc36cc1edb
    当使用 --server 运行时,一个简单的异步服务器正在运行。为了模拟处理负载,每个请求都被处理为一个最大值。 3 秒颠簸:await asyncio.sleep(randrange(3000) / 1000)为了模拟您的问题,客户端可以运行指定数量的并行请求。
    示例运行:

    $ asyncio-ordering-PoC/tester.py --client --count 5
    2020-09-23 10:28:10,539 [MainThread ] [INFO ] Running client to localhost TCP-port: 8888
    2020-09-23 10:28:10,541 [MainThread ] [DEBUG] Send: Test 1
    2020-09-23 10:28:10,541 [MainThread ] [DEBUG] Send: Test 2
    2020-09-23 10:28:10,541 [MainThread ] [DEBUG] Send: Test 3
    2020-09-23 10:28:10,541 [MainThread ] [DEBUG] Send: Test 4
    2020-09-23 10:28:10,541 [MainThread ] [DEBUG] Send: Test 5
    2020-09-23 10:28:11,105 [MainThread ] [DEBUG] Received: Got: 'Test 2'
    2020-09-23 10:28:11,105 [MainThread ] [DEBUG] Close the socket
    2020-09-23 10:28:11,443 [MainThread ] [DEBUG] Received: Got: 'Test 4'
    2020-09-23 10:28:11,444 [MainThread ] [DEBUG] Close the socket
    2020-09-23 10:28:12,404 [MainThread ] [DEBUG] Received: Got: 'Test 1'
    2020-09-23 10:28:12,404 [MainThread ] [DEBUG] Close the socket
    2020-09-23 10:28:12,619 [MainThread ] [DEBUG] Received: Got: 'Test 5'
    2020-09-23 10:28:12,619 [MainThread ] [DEBUG] Close the socket
    2020-09-23 10:28:12,922 [MainThread ] [DEBUG] Received: Got: 'Test 3'
    2020-09-23 10:28:12,922 [MainThread ] [DEBUG] Close the socket
    2020-09-23 10:28:12,922 [MainThread ] [INFO ] Done.
    服务器日志:
    2020-09-23 10:28:10,541 [MainThread  ] [DEBUG]  Received Test 1 from ('127.0.0.1', 47252)
    2020-09-23 10:28:10,541 [MainThread ] [DEBUG] Received Test 2 from ('127.0.0.1', 47254)
    2020-09-23 10:28:10,541 [MainThread ] [DEBUG] Received Test 3 from ('127.0.0.1', 47256)
    2020-09-23 10:28:10,541 [MainThread ] [DEBUG] Received Test 4 from ('127.0.0.1', 47258)
    2020-09-23 10:28:10,541 [MainThread ] [DEBUG] Received Test 5 from ('127.0.0.1', 47260)
    2020-09-23 10:28:11,104 [MainThread ] [DEBUG] Sending after delay of 562 ms: Test 2
    2020-09-23 10:28:11,443 [MainThread ] [DEBUG] Sending after delay of 901 ms: Test 4
    2020-09-23 10:28:12,404 [MainThread ] [DEBUG] Sending after delay of 1860 ms: Test 1
    2020-09-23 10:28:12,618 [MainThread ] [DEBUG] Sending after delay of 2076 ms: Test 5
    2020-09-23 10:28:12,921 [MainThread ] [DEBUG] Sending after delay of 2379 ms: Test 3

    关于python - Python asyncio Streams 是否维护多个作者和读者的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64017656/

    27 4 0