http客户端关闭连接时如何存储响应?

瓦列里·索洛维约夫(Valeriy Solovyov)

我在python中使用falcon框架来形成Web API的json响应。

例如,我有一个函数logic()该函数在30-90分钟内有效。我想要这样的东西:

  1. 当http客户端要求/api/somepath.json时,我们会调用 somepath_handle()
  2. somepath_handle()logic()在另一个线程/进程中运行
  3. logic()完成后,线程被关闭
  4. somepath_handle()读取logic()来自return的响应
  5. 如果somepath_handle()logic()完成之前被杀死,则logic()直到完成的线程/ etc才会停止

代码:

def somepath_handle():
    run_async_logic()
    response=wait_for_async_logic_response() # read response of logic()
    return_response(response)
瓦列里·索洛维约夫(Valeriy Solovyov)

我正在使用一个简单的工作程序来创建我正在处理一些命令的队列。如果添加简单的响应存储,则有可能处理任何请求,并且在连接断开时不会丢失请求。

示例:使用falconframework.org响应请求的主要功能。

main.py:

from flow import Flow
import falcon
import threading
import storage

__version__ = 0.1
__author__ = '[email protected]'


app = falcon.API(
    media_type='application/json')

app.add_route('/flow', Flow())

THREADS_COUNT = 1
# adding the workers to process queue of command
worker = storage.worker
for _ in xrange(THREADS_COUNT):
    thread = threading.Thread(target=worker)
    thread.daemon = True
    thread.start()

使用工作人员代码storage.py进行简单的存储:

from Queue import Queue
import subprocess
import logging
main_queque = Queue()

def worker():
    global main_roles_queque
    while True:
        try:
            cmd = main_queque.get()
            #do_work(item)
            #time.sleep(5)
            handler = subprocess.Popen(
            cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
            stdout, stderr = handler.communicate()
            logging.critical("[queue_worker]: stdout:%s, stderr:%s, cmd:%s" %(stdout, stderr, cmd))
            main_queque.task_done()
        except Exception as error:
            logging.critical("[queue_worker:error] %s" %(error))

该类将处理所有请求[POST,GET] flow.py:

import storage
import json
import falcon
import random

class Flow(object):

    def on_get(self, req, resp):
        storage_value = storage.main_queque.qsize()
        msg = {"qsize": storage_value}
        resp.body = json.dumps(msg, sort_keys=True, indent=4)
        resp.status = falcon.HTTP_200

    #curl -H "Content-Type: application/json" -d '{}'  http://10.206.102.81:8888/flow
    def on_post(self, req, resp):
        r = random.randint(1, 10000000000000)
        cmd = 'sleep 1;echo "ss %s"' % str(r)
        storage.main_queque.put(cmd)
        storage_value = cmd
        msg = {"value": storage_value}
        resp.body = json.dumps(msg, sort_keys=True, indent=4)
        resp.status = falcon.HTTP_200

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

码头如何检测,如果HTTP连接被关闭客户端

Python HTTP Server /客户端:远端关闭连接,没有响应错误

如何关闭AWS S3客户端连接

如何正确处理HTTP文件服务器上的客户端“连接:关闭”请求?

如何非破坏性地检查HTTP客户端是否已关闭连接?

如何访问http客户端响应正文全局

Jersey客户端是否在发生异常时关闭连接?

从tomcat中的Filter类方法响应servlet客户端时如何设置http状态代码

Http客户端现有连接被远程主机强行关闭

Python 3 asyncio如何正确关闭客户端连接

每次通话均与python tornado HTTP客户端关闭连接

检查客户端是否关闭连接时,reset()出错

如何关闭SignalR中的客户端JavaScript“ Hub”连接?

Prometheus关闭时,Prometheus客户端库的行为如何?

nginx错误-等待请求时客户端关闭连接,客户端:xxxx,服务器:0.0.0.0:80

WebSocket客户端在连接时关闭

检测客户端关闭连接Blazor

如何处理客户端EventSource.close()关闭的反应流HTTP连接上的ClosedChannelException?

如果客户端在10秒内没有响应,如何通过超时关闭连接?

必须HTTP客户端关闭其连接

当我无权访问服务器时,如何从客户端关闭套接字连接?

如何关闭WCF客户端连接

使用HTTP客户端时流关闭

如何强制关闭客户端连接Rabbitmq

TcpInboundGateway:如何关闭与远程客户端的现有连接

我如何连接http客户端

弹簧靴。反应式网络客户端。连接在响应前提前关闭

当客户端关闭连接时,Go http.ResponseWriter.Write 不会返回错误

如何在 ASIO 中关闭异步客户端连接?