Discussion:
[staf-users] HOW to run python code in STAX XML Program
Raja Narayanan
2011-03-18 13:43:17 UTC
Permalink
Hello Staf-users,

Im trying to run the following python code in STAX XML program within <script>. But facing RC:4001 error submitting executing request problem.

<script>
         import os
         import signal
         import subprocess
   
       # Change this to your process name
          processname = "server"

          def findThisProcess(process_name):
          ps = subprocess.Popen("ps -eaf | grep "+process_name, shell=True, stdout=subprocess.PIPE)
          output = 'ps.stdout.read()'
          ps.stdout.close()
          ps.wait()
          return output
 </script>

Q1: Does the method I have followed is correct or not?
Q2: Do we need to set up python installer path or env variable need to set before running the python code.

Please help

Thanks,
V Raja
David Bender
2011-03-18 14:08:33 UTC
Permalink
Hi,

First, you need to understand that STAX uses Jython 2.1, which is
equivalent to Python 2.1. The Python documentation for subprocess
(http://docs.python.org/library/subprocess.html#subprocess-replacements)
indicates that it was added in Python 2.4, so it is not available with
Jython 2.1. Furthermore, in the STAX User's Guide section "Appendix F:
Jython and CPython
Differences" (http://staf.sourceforge.net/current/STAX/staxug.html#Header_CPythonDiffs)
it is documented that "Some standard CPython modules depend on operating
system calls that are not available under Java. The most notable of these
is os, which actually does run in Jython, but is missing much of its
functionality."

There are other ways you could do this. For example, you could just run
the "ps -ea...." via a <process> element. For example:

<process>
<location>'local'</location>
<command mode="'shell'">'ps -eaf | grep %s' %
(processname)</command>
<stderr mode="'stdout'"/>
<returnstdout/>
</process>

<message log="1">STAXResult[0][1]</message>

Or, if you really wanted to run your Python code, you could have it in
a .py file on the system (and have Python 2.4 or later installed on the
system), and execute it via a process element:

<process>
<location>'local'</location>
<command mode="'shell'">'/opt/Python-2.4/bin/
python /tests/myscript.py'</command>
<stderr mode="'stdout'"/>
<returnstdout/>
</process>

Thanks,
David



David Bender 11501 Burnet Rd. Phone (T/L): 1-512-286-5315
STAF/STAX Development Bldg. 903-5B002 (363-5315)
Austin, TX ITN: 23635315
IBM Software Group, 78758-3400 Email: ***@us.ibm.com
WPLC









From: "Raja Narayanan" <***@rediffmail.com>

To: "staf-users " <staf-***@lists.sourceforge.net>

Date: 03/18/2011 08:45 AM

Subject: [staf-users] HOW to run python code in STAX XML Program






Hello Staf-users,

Im trying to run the following python code in STAX XML program within
<script>. But facing RC:4001 error submitting executing request problem.

<script>
import os
import signal
import subprocess

# Change this to your process name
processname = "server"

def findThisProcess(process_name):
ps = subprocess.Popen("ps -eaf | grep "+process_name, shell=True,
stdout=subprocess.PIPE)
output = 'ps.stdout.read()'
ps.stdout.close()
ps.wait()
return output
</script>

Q1: Does the method I have followed is correct or not?
Q2: Do we need to set up python installer path or env variable need to set
before running the python code.

Please help

Thanks,
V Raja
Sharon Lucas
2011-03-18 14:44:18 UTC
Permalink
You should post the error message provided not just the return code. I'm
guessing you received a STAXPythonEvaluationError, because Python code
blocks are defined by their indentation. By "code block", I mean
functions, if statements, for loops, while loops, and so forth. Indenting
starts a block and unindenting ends it. There are no explicit braces,
brackets, or keywords. This means that whitespace is significant, and must
be consistent.

So, if you really have the following Python code within a <script>
element, you'll get a STAXPythonEvaluationError because the line with the
comment (#) does not start at the same indentation as the rest of the
code. And the code after it is also not at the same indentation level.
And the Python function you defined is not indented properly, etc.

Note this is in addition to the comments that David made already about how
STAX V3.4.5 and earlier use Jython 2.1 to execute the Python code.

<script>
import os
import signal
import subprocess

# Change this to your process name
processname = "server"

def findThisProcess(process_name):
ps = subprocess.Popen("ps -eaf | grep "+process_name,
shell=True, stdout=subprocess.PIPE)
output = 'ps.stdout.read()'
ps.stdout.close()
ps.wait()
return output
</script>

--------------------------------------------------------------
Sharon Lucas
IBM Austin, ***@us.ibm.com
(512) 286-7313 or Tieline 363-7313




From: "Raja Narayanan" <***@rediffmail.com>
To: "staf-users " <staf-***@lists.sourceforge.net>
Date: 03/18/2011 08:45 AM
Subject: [staf-users] HOW to run python code in STAX XML Program



Hello Staf-users,

Im trying to run the following python code in STAX XML program within
<script>. But facing RC:4001 error submitting executing request problem.

<script>
import os
import signal
import subprocess

# Change this to your process name
processname = "server"

def findThisProcess(process_name):
ps = subprocess.Popen("ps -eaf | grep "+process_name,
shell=True, stdout=subprocess.PIPE)
output = 'ps.stdout.read()'
ps.stdout.close()
ps.wait()
return output
</script>

Q1: Does the method I have followed is correct or not?
Q2: Do we need to set up python installer path or env variable need to set
before running the python code.

Please help

Thanks,
V Raja

Loading...