如何在Python线程中中止/取消HTTP请求?

我正在寻找中止/取消 Python 线程中的 HTTP 请求。我必须坚持使用线程。我无法使用 asyncio 或标准库之外的任何内容。


此代码适用于套接字:


"""Demo for Canceling IO by Closing the Socket


Works!


"""


import socket

import time


from concurrent import futures


start_time = time.time()


sock = socket.socket()



def read():

    "Read data with 10 second delay."

    sock.connect(('httpbin.org', 80))

    sock.sendall(b'GET /delay/10 HTTP/1.0\r\n\r\n')

    while True:

        data = sock.recv(1024)

        if not data:

            break

        print(data.decode(), end='')



with futures.ThreadPoolExecutor() as pool:

    future = pool.submit(read)

    futures.wait([future], timeout=5)

    sock.close()  # <-- Interrupt sock.recv(1024) in Thread:read().


end_time = time.time()

print(f'Duration: {end_time - start_time:.3f}')


# Duration is ~5s as expected.

在主线程中关闭套接字用于中断执行器池线程中的recv()。HTTP 请求应该需要 10 秒,但我们只等待 5 秒,然后关闭套接字(有效地取消 HTTP 请求/响应)。


现在我尝试使用 http.client:


"""Demo for Canceling IO in Threads with HTTP Client


Doesn't work!


"""


import time


from concurrent import futures


from http.client import HTTPConnection



def get(con, url):

    con.request('GET', url)

    response = con.getresponse()

    return response



start_time = time.time()


with futures.ThreadPoolExecutor() as executor:

    con = HTTPConnection('httpbin.org')

    future = executor.submit(get, con, '/delay/10')

    done, not_done = futures.wait([future], timeout=5)

    con.sock.close()


end_time = time.time()

print(f'Duration: {end_time - start_time:.3f}')


# Duration is ~10s unfortunately.

不幸的是,这里的总持续时间约为 10 秒。关闭套接字不会中断客户端中的recv_into()。


看来我做出了一些错误的假设。如何从单独的线程中断 http 客户端中使用的套接字?


莫回无
浏览 68回答 1
1回答

DIEA

您所描述的是预期的有据可查的行为:注意 close() 释放与连接关联的资源,但不一定立即关闭连接。如果要及时关闭连接,请在 close() 之前调用 shutdown()。有关此行为的一些进一步详细信息仍然可以在 CPython howto 文档中找到:严格来说,您应该在关闭套接字之前对其使用 shutdown 。关闭是对另一端套接字的建议。根据您传递的参数,它可能意味着“我不会再发送,但我仍然会听”,或者“我不听,很好的摆脱!”。然而,大多数套接字库已经习惯了程序员忽略使用这一礼仪,通常关闭与 shutdown() 相同;关闭()。因此在大多数情况下,不需要显式关闭。有效使用 shutdown 的一种方法是使用类似 HTTP 的交换。客户端发送请求,然后执行关闭(1)。这告诉服务器“该客户端已完成发送,但仍然可以接收。”&nbsp;服务器可以通过接收 0 字节来检测“EOF”。它可以假设它具有完整的请求。服务器发送回复。如果发送成功完成,那么客户端确实仍在接收。Python 在自动关闭方面更进了一步,它表示当套接字被垃圾回收时,如果需要它会自动关闭。但依赖这一点是一个非常不好的习惯。如果您的套接字在没有执行关闭的情况下就消失了,另一端的套接字可能会无限期地挂起,认为您只是速度慢。完成后请关闭套接字。解决方案关闭之前调用 shutdown。例子with&nbsp;futures.ThreadPoolExecutor()&nbsp;as&nbsp;executor: &nbsp;&nbsp;&nbsp;&nbsp;con&nbsp;=&nbsp;HTTPConnection('httpbin.org') &nbsp;&nbsp;&nbsp;&nbsp;future&nbsp;=&nbsp;executor.submit(get,&nbsp;con,&nbsp;'/delay/10') &nbsp;&nbsp;&nbsp;&nbsp;done,&nbsp;not_done&nbsp;=&nbsp;futures.wait([future],&nbsp;timeout=5) &nbsp;&nbsp;&nbsp;&nbsp;con.sock.shutdown() &nbsp;&nbsp;&nbsp;&nbsp;con.sock.close()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python