PlatesOut
PlatesOut

Reputation: 21

How can I use subprocess.Popen to run a method in another file while passing in multiple dictionaries?

p = subprocess.Popen(['python2.7', 'from some.path.foo import run_setup', 'import ast',
                      'nsvS = ' + str(new_system_variables), 'nsv = ast.literal_eval(nsvS)',
                      'uS = ' + str(user_dict), 'u = ast.literal_eval(uS)',
                      'run_setup(nsv, u)'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Above is my code. I need to have the run_setup method run in the background. run_setup does in fact run with my current implementation, but none of the values in the dictionaries seem to get passed in. new_system_variables and user_dict being the 2 dictionaries I'm trying to pass in. Thanks in advance!

Upvotes: 2

Views: 355

Answers (2)

Roland Smith
Roland Smith

Reputation: 43523

Use multiprocessing instead;

from multiprocessing import Process
from some.path.foo import run_setup

p = Process(target=run_setup, args=(new_system_variables, user_dict))
p.start()
p.join()

Upvotes: 4

Steve Barnes
Steve Barnes

Reputation: 28405

You need to either, or both, remove the spaces around the =s in you string format or put double quotes around them, the latter is better as it will also cope with any spaces in the parameters.

You may also need to have a '-c' option to tell python that the following is a command.

Upvotes: 0

Related Questions