5

I would like to know how i can handle it, that i get a variable/value from the subprocess to the parent.

I am running the subprocess as an script. The parent looks like:

import subprocess
p = subprocess.Popen('abaqus python getData.py', shell=True)
p_status = p.wait()
print b

The child looks like:

from numpy import *

if __name__ == "__main__":

    b = [0,1,2,3]  # output is a list of integers
    global a = b

I am totally new to python. I think the problem is, that i can not store variables this way and make them "public" to the parent? Do i have to write them in a *.txt or something like that and get them with numpy.loadtxt()?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Nils
  • 409
  • 6
  • 16
  • those are 2 different processes. You cannot share the data like this. do you _really_ have to create another process? – Jean-François Fabre Jan 11 '18 at 14:18
  • @Jean yeahh, i have to do so, cause abaqus works with his own python compiler and package "odbAccess". with that i can wrap data from an *.odb file, but i can not import the package "metaplotlib" as needed. so i start the python compiler, run the abaqus python compiler through the parant and would like to give the values back to the parent, so that i can make some plots from the data. btw. i am sorry for my english. – Nils Jan 11 '18 at 14:23
  • what is the type of the object you're returning from the subprocess? – Jean-François Fabre Jan 11 '18 at 14:33
  • its a list with integers – Nils Jan 11 '18 at 14:40
  • @Jean-FrançoisFabre any idea how I can handle that? Thanks :) – Nils Jan 11 '18 at 18:19
  • If it's just a list of integers, they're easy to serialize -- ie. pass over stdout. – Charles Duffy Jan 11 '18 at 19:19
  • @CharlesDuffy thanks, could u be so kind and give me a short example? I am really totally new to python. Thank you very much. If u got no time, it’s ok. I will check google for that. ^^ – Nils Jan 11 '18 at 19:30
  • An example is already available to you: The answer by Jean-François Fabre does what I propose, serializing the array as a Python datastructure, printing it to stdout, and parsing it in the parent. (If you wanted to be able to implement one component or the other in a language other than Python, I'd suggest standardizing on JSON instead). – Charles Duffy Jan 11 '18 at 19:32
  • @CharlesDuffy Ok, thank you so much! – Nils Jan 11 '18 at 19:35

1 Answers1

4

Even if the subprocess is a python process, you cannot communicate between them using global variables.

You cannot use a multiprocessing.Manager object either since you need 2 distinct python engines (with separate capabilities).

The classic (and easy) way is to serialize your data using a simple print (since you have a list of integers in output), and deserialize using ast.literal_eval (json would also be possible)

Callee (getData.py mockup):

if __name__ == "__main__":
    print([1,2,3,4])  # mock output

Caller:

import subprocess,ast
output = subprocess.check_output('abaqus python getData.py', shell=True)
lst = ast.literal_eval(output.decode("ascii"))

now lst contains [1,2,3,4]

If your output data is more complex (list of lists, dictionaries...) just compose the list of lists, dictionary with nested dicts inside, ... and print that. ast.literal_eval is able to rebuild the same structure on the other side.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219