mod_wsgi and calling subprocess.Popen

601 views
Skip to first unread message

yurkao

unread,
Sep 19, 2010, 7:35:11 PM9/19/10
to modwsgi
i've ported my simple django application (v0.96 on Ubuntu hardy 8.04)
from mod_python to mod_wsgi.
the application seems to be simple enough.
On each Http user request it runs sequentaly external system commands:
* ps aux
* cat /proc/<pid>/maps
and writes their output to same output file.

however, the result output file holds only ps aux call output, and not
the both.
Any ideas how to correctly implement that with mod_wsgi?

the code:

The code:
from django.http import HttpResponse
from subprocess import Popen

import threading
import os
import sys
import subprocess

def ps_aux():
"""
print entire process table
"""
proc = subprocess.Popen("ps
aux",shell=True,bufsize=0,stdout=sys.stdout)
proc.wait()

def cat_maps(pid):
"""
print process memory maps
"""
maps = "/proc/%s/maps"%(pid)
proc = subprocess.Popen("cat %s"%
(maps) ,shell=True,bufsize=0,stdout=sys.stdout)
proc.wait()

def external(*args,**kwargs):
pid = os.fork()
if pid:
os.waitpid(pid,0)
return

log = open("/var/www/ext-%s.log" %(os.getpid()),"a",0)
log.truncate()

os.dup2(log.fileno(),1)
os.dup2(log.fileno(),2)

sys.stdout = log

ps_aux()
cat_maps(os.getpid())

log.flush()
log.close()
sys.exit()

def start():
thread = threading.Thread(target=external)
thread.setDaemon(True)
thread.start()

def inde(request):
start()
return HttpResponse('Hello!')

Robert Coup

unread,
Sep 19, 2010, 8:52:37 PM9/19/10
to mod...@googlegroups.com
On Mon, Sep 20, 2010 at 11:35 AM, yurkao <yur...@gmail.com> wrote:
i've ported my simple django application (v0.96 on Ubuntu hardy 8.04)
from mod_python to mod_wsgi.
the application seems to be simple enough.
On each Http user request it runs sequentaly external system commands:
 * ps aux
 * cat /proc/<pid>/maps
and writes their output to same output file.

however, the result output file holds only ps aux call output, and not
the both.
Any ideas how to correctly implement that with mod_wsgi?

Why not read the command output directly from the subprocess rather than from the webserver log? 

p = subprocess.Popen(...other args..., stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout,stderr = p.communicate()
stdout_content = stdout.read() # stdout is a file-like object
stderr_content = stderr.read()

if you want stderr to be combined in a single pipe with stdout, use stderr=subprocess.STDOUT in your Popen() arguments.

Rob :)

Rob :)

yurkao

unread,
Sep 19, 2010, 9:26:08 PM9/19/10
to modwsgi
Rob, not sure if i've understood you corectly, but this is not
webserver log. this is a sample job user wants to perform periodicaly.
Its output should be redirected to some __output__ file, independent
from webserver\apache\django log file.
consider user has a library (module) with some method. when user calls
Django's my_view - the user's module method is called.
method can use:
1) print statements
2) subprocess.Popen
to implement that without modifing user's module, the only logical way
is to make fork+dup2+module call.

in case of os.dup2 - all stdout output will be redirected to output
file. that what is want.
the problem that second subprocess.Popen call's output (in my code)
is not saved in desired output file (and even not saved\appers
anywhere in Apache\virtual host logs) when using mod_wsgi.

Graham Dumpleton

unread,
Sep 19, 2010, 9:28:49 PM9/19/10
to mod...@googlegroups.com
Why not write a self contained shell script which does everything you
want? Then invoke that using os.system() or similar.

Certainly a lot easier as you don't have to fiddle with executing all
the separate parts from Python.

Graham

> --
> You received this message because you are subscribed to the Google Groups "modwsgi" group.
> To post to this group, send email to mod...@googlegroups.com.
> To unsubscribe from this group, send email to modwsgi+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/modwsgi?hl=en.
>
>

yurkao

unread,
Sep 19, 2010, 9:43:14 PM9/19/10
to modwsgi
1) that was just an example. The reality may be much more complex.
2) sometimes it cannot be combined into "shell" script; Interracting
with DB between above calls ( ps_aux and cat_maps ) , accessing SVN
other web site, local data etc,i've just simplified the cased.

yurkao

unread,
Sep 19, 2010, 9:39:12 PM9/19/10
to modwsgi
1) that was just an example. The reality may be much more complex.
2) sometimes it cannot be combined into "shell" script; Interracting
with DB between above calls ( ps_aux and cat_maps ) , accessing SVN
other web site, local data etc,i've just simplified the cased.
On 20 сен, 03:28, Graham Dumpleton <graham.dumple...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages