user1604594
user1604594

Reputation: 93

using wsgi script to call exe

I'm running apache, Django and wsgi. I also use this other software called SAS to do statistical analysis. Just to give you some context. My end goal is when a client hits submit on a form written in the django, the appropriate sas script is called (via a python wsgi script) which performs calculations on the server, and then redirects the client to the output page.

I have a basic script called test5.py. It looks like this:

import os
import subprocess

def application(environ, start_response):

  status = '200 OK'
  output = 'Running External Program!'

  f = open("C:\Documents and Settings\eric\Desktop\out.txt", 'a')
  f.write('hi')
  f.close()

  #os.system(r'start "C:\Program Files\SAS92\SASFoundation\9.2\sas.exe"')
  #subprocess.call([r'C:\Program Files\SAS92\SASFoundation\9.2\sas.exe'])
  #os.startfile(r'C:\Program Files\SAS92\SASFoundation\9.2\sas.exe')
  response_headers = [('Content-type', 'text/plain'),
                      ('Content-Length', str(len(output)))]

  #start_response('301 Redirect', [('Location', 'http://myserver/reports'),])
  start_response(status, response_headers)

  return [output]

So what happens is that the out.txt file does get created and have hi written in the file. That's quite cool. The first 3 commented lines were 3 attempts to have this same script also call sas.exe which lives on the server. I'm just trying to get any .exe to work right now, so calling paint or wordpad would be fine. Those lines however do not seems to execute in the wsgi context. If I just load the Python command line, I can get the .exes to execute just fine. Also the last comment seems to be working properly in the redirecting. I'm not sure if I need to configure apache to add executables. Please forgive me if I'm using terms incorrectly. I am still quite new to all of this.

Thanks


Hi Paulo,

I was trying to look into your last comment. I am a bit confused as to exactly what i am looking for or how to look for it. Here is some information that I have gathered. By the way i am running on windows XP and using Apache 2.2.

My apache is installed for all users as in regedit the variable ServerRoot is under HKEY_LOCAL_MACHINE (http://httpd.apache.org/docs/2.2/platform/windows.html). Also I believe SAS is installed under all users. I tested this by having my coworker sign in using her login and I still had access. I’m not sure if that is a sufficient test though.

The log I get when I run the wsgi is the following. I’m not sure if it matters that the process is empty.

[Mon Aug 20 10:33:17 2012] [info] [client 10.60.8.71] mod_wsgi (pid=5980, process='', application='..com|/test5'): Reloading WSGI script 'C:/Sites/cprm/pyscripts/test5.wsgi'.

Also I tried the .bat trick from the link I posted in the comment i posted earlier to no avail. I made a simple batch file that just echoes 'hi' and placed it in the same directory where my wsgi scripts live. I feel like there should be no access problems there, but I may be mistaken. I also just tried calling a simple python script using subprocess just to test. Also nothing happened.

Also just to show you, my httpd.conf file looks like such:

AllowOverride None Options None Order allow,deny Allow from all

WSGIScriptAlias /test1 "C:/sites/cprm/pyscripts/test1.wsgi" WSGIScriptAlias /test2 "C:/sites/cprm/pyscripts/test2.py" WSGIScriptAlias /test3 C:/sites/cprm/pyscripts/test3.py WSGIScriptAlias /test4 "C:/sites/cprm/pyscripts/test4.py" WSGIScriptAlias /test5 "C:/sites/cprm/pyscripts/test5.wsgi" WSGIScriptAlias / "C:/sites/cprm/wsgi.py"

Is this information helpful or not really? Also, am i looking for a specific environ variable or something?

Thanks again

Upvotes: 2

Views: 1748

Answers (1)

Paulo Scardine
Paulo Scardine

Reputation: 77339

For web applications that perform background calculations or other tasks, IMHO it is best to queue the tasks for processing instead of calling an external process from a Django view and hang everything until the task completes. This leads to better:

  • user experience (request returns instantly - use ajax to signal task status and present the download link once task completes)
  • security (background process can run under safer credentials)
  • scalability (tasks can be distributed among servers)
  • resilience (by default many webservers will send an 'error 500' if your application fails to answer under 30 seconds or so)

For a background daemon processing all entries in the queue, there are several approaches depending on how big you want to scale:

[edit]

The process you start from a WSGI script will run under the same user that is running the webserver. In linux it is generally 'www-data' or 'nobody', in Windows/IIS it is 'IUSR_MachineName' (or authenticated user if using IIS authentication). Check if you can start the program using the same credentials your WSGI is running under.

Upvotes: 1

Related Questions