1
votes

We have a WSGI application with Python3 running under Apache Linux.

We want to interact with an external API after acknowledging a request / notification received via the Web server

Sample WSGI python code:

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") 

Our code is slightly different to the sample code (using Werkzeug).

What is the best way to solve this? We are purposefully not using any frameworks (except Werkzeug) and we want to avoid large changes in architecture (thousands of lines of code)

Thank you, Kris

1
Thanks @GrahamDumpleton - those options are a bit new to me. Using "try" and "finally" in the Exception framework is an option,- Im wondering if using the Exception framework in an unintended manner will cause headaches later. Eg if one wants to initiate a sequence of RESTful interactions. You indicate that "atexit" may not be portable, so maybe less desirable.Kris van der Merwe
Is the difference between WSGI and Python RESTful the decoupling of the HHTP connection from the thread? I wonder if there is something like a simple Python RESTful module that one could attach to an Apache Alias/Directory?Kris van der Merwe
The atexit callbacks only help when a process is shutdown. A process survives more than one request usually and so is of no use. WSGI defines an interface between a web server and a Python web application. a RESTful application is a style of writing an application. It needs something like WSGI to interface to the web server. So you are going to need WSGI and a server implementing the WSGI specification to be able to talk to most ways of hosting Python web applications. Your only other choice is Python async frameworks which implement own API.Graham Dumpleton
I had a play with “try” “finally”, but could not get it it working for this pattern. A Python return in “try” gets discarded when Python processes “finally”. As the return from the “try” code is discarded the initial transaction is never concluded. I had a quick read on Python asynchronous frameworks but not sure if they are a good fit. Im trying to implement synchronous transactions – conclude a transaction and then initiate the next.Kris van der Merwe

1 Answers

0
votes

I implemented a solution by creating a new Python thread and attaching the second transaction to it. To ensure it kicks of after the first transaction, i put a small delay in the thread before it starts the second transaction. Hoping there are no issues introduced with threading.