首页 文章

WSGI具有RESTful后处理功能

提问于
浏览
1

我们有一个在Apache Linux下运行的Python3的WSGI应用程序 .

我们希望在确认通过Web服务器收到的请求/通知后与外部API进行交互

示例WSGI python代码:

def application(environ, start_response):
    path=  environ.get('PATH_INFO', '')   
    if path == "/ProcessTransact":
        import sys
        sys.stderr.write("Entering /ProcessTransact, Checking validity ...\n" )
        # Get the context of the notification/request from Post parameters etc, assume it is a valid ...
        status = '200 OK'  
        body = b"Acknowledge the valid submit with '200 OK'"
        response_headers = [
            ('Content-Type', 'text/html'),
            ('Content-Length', str(len(body)))
        ]

        start_response(status, response_headers)
        return [body]

        # we have acknowledged the context of the above request
        # we want to do an HTTP POST based on the context 
        # When we return [body], we lost the processing thread

        import requests           #or maybe something else
        sys.stderr.write("POST RESTful transactions here after acknowledging the request (we never get here).\n")

我们的代码与示例代码略有不同(使用Werkzeug) .

解决这个问题的最佳方法是什么?我们故意不使用任何框架(Werkzeug除外),我们希望避免架构中的大变化(数千行代码)

谢谢你,克里斯

1 回答

  • 0

    我通过创建一个新的Python线程并将第二个事务附加到它来实现了一个解决方案 . 为了确保在第一次事务之后它被踢,我在线程开始第二次事务之前稍微延迟了 . 希望线程没有引入任何问题 .

相关问题