Python:普通套接字和http连接上的异步发送/接收

2024-10-03 17:18:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个任务,需要在前端接受msg(普通字符串),做一些处理,并将这些msg转换为http请求,这些请求需要发送到后端服务器。所以基本上有两个端点-前端是一个普通的BSD套接字。后端是urllib.HTTPconnection. 反应正好相反。但是,这不是请求-响应场景。我可能会有一个完全坏掉的请求1->;req2->;req4->;req3->;resp3->;resp1。。。异步方案的类型。所以我不能只做一个:

msg = socketFrontEnd.recv()
... process msg and make 'Request' 
resp, content = httpBackEnd.request("http://example.org", "PUT", body=Request)   
... process resp and make 'Response'
socketFrontEnd.send(Response)

我需要一个更像投票机制的东西:

^{pr2}$

然而,据我所知,我不能将http连接放在某种select语句中。即使这是可能的,如何从httpconnection分别发送和接收(而不是使用单个“http.请求(..“命令)。在

如果我在httpconnection上发送一些ajax请求,如果我在socketFrontEnd上被阻止,ajax请求中的回调会运行吗?也就是说,我们能做些什么吗

while(True):
    msg = socketFrontEnd.recv() <-- blocked
    ... process and make 'Request'
    ajax('callback_function', Request, 'http://backendserver.com')


Callback_function(resp):
    ... process and make Response
    socketFrontEnd.send(Response)

Tags: andgtsendhttpmakeresponserequestajax