Search code examples
javascriptpythonsubprocessstdoutpyramid

Dynamically output stdout from Pyramid views' subprocess.Popen to web page


I am using the Pyramid web framework to serve a performance model and allow a number of users to use it remotely.

In short, parameters provided by the user are input to an XML file and then the model, which is written in C++ and uses the XML, is executed with subprocess.Popen in a separate view.

The model can take some time and its logging information is valuable to the user. I was hoping I could write each line from the stdout to an HTML file and then generate an iFrame with this file as the source.

Once the subprocess has begun, an iFrame is generated with the HTML file being updated as the source

<iframe id="logSimInlineFrame" src="${request.static_url('fcmod_web:temp/logfile.html')}" Content-Type="text/plain" charset="utf-8"></iframe>

I realize this isn't static and I encounter the following error

ValueError: No static URL definition matching fcmod_web:temp/logfile.html

So my question is, am I on the right track with an iFrame whose content is generated by Python? And if so, how should I provide this data so it is updated dynamically?

Or, and I imagine this to be the case, is there a much more efficient way to stream the data from the stdout PIPE to a frame on the web page?


Solution

  • IFrame streaming is hacky as the best. The techniques you can utilize for real-time communications with browser

    Techniques like AJAX and HTTP long polling are not designed for streamable communication. All modern browsers support WebSockets natively - the last browser which did not support them is Android 2.2.

    For streaming your normal web server probably won't cut it, so you need to explore e.g. uWSGI and Server-Sent event support. Please note that Python does not have standard for real-time communications, unlike WSGI for HTTP, so any solution will be a specific to your web server software.