通过请求线程化 HTTP post,无需等待请求通过

Threaded HTTP post via requests without waiting for request to go through

我的程序每 250 毫秒左右发送一次 http post,我希望它在请求通过时保持 运行。本质上,我正在寻找类似文件的系统,它只是发送请求(可能在不同的线程中)并继续运行而不等待服务器的响应。

该程序类似于:

while True:
    value_to_send = some_function()
    x = requests.post(url, json = myjson) # this json has the updated value_to_send in it 

您可以使用线程,而不必等待它们完成:

from threading import Thread
import time

def request():
    # value_to_send = some_function()
    # x = requests.post(url, json = myjson)
    print('started')
    time.sleep(.5)
    print("request done!")


def main():
    while True:
        t = Thread(target=request)
        t.start()
        time.sleep(.25)

main()

输出:

started
started
request done!
started
request done!
started
request done!
...