Running jython program from python using subprocess module?

I have a jython script server (called rajant_server.py) that interacts with the java api file to communicate through special network radio stations. I have a python program that acts as a client (and also does a few other things). Currently, I have to start the server first by opening a command / terminal window and typing:

cd [path to directory containing rajant_server.py jython rajant_server.py 

As soon as the server successfully connects, it waits for the client that I start:

 cd [path to directory containing python client program] python main.py 

When a client connects, the server prints information (currently for debugging) in this command / terminal window, and the client program displays debugging information in this command / terminal window. What I want to do is get rid of the complicated process by calling jython from my main.py program using the subprocess module.

The task consists of two times:

1 - I need the rajant_server.py program to open my own terminal / command window in it

2 - jython should run in the directory where the rajant_server.py file is stored, in other words, entering the command line / Terminal Window does not work (do not ask me why):

 jython C:/code_dir/comm/server/rajant_server.py 

a

 cd C:/code_dir/comm/server jython rajant_server.py 

works.


Alright ... I have something to work on. It seems like a hack, so I will still love ideas for a better approach. Here is what I am doing now:

 serverfile = r'rajant_server_v2.py' serverpath = os.path.join(os.path.realpath('.'),'Comm',serverfile) serverpath = os.path.normpath(serverpath) [path,file] = os.path.split(serverpath) command = '/C jython '+file+'\n' savedir = os.getcwd() os.chdir(path) rajantserver = subprocess.Popen(["cmd",command],\ creationflags = subprocess.CREATE_NEW_CONSOLE) #Change Directory back os.chdir(savedir) #Start Client rajant = rajant_comm.rajant_comm() rajant.start() 

If you have a solution that will work on both Linux and Windows, you will become my hero. For some reason, I could not change the stdin or stdout specifications in the subprocess when I added createflags = subprocess.CREATE_NEW_CONSOLE.

+4
source share
1 answer

The Popen function in the subprocess takes the optional parameter 'cwd' to determine the current working directory of the child process.

 rajantserver = subprocess.Popen(["cmd",command],\ creationflags = subprocess.CREATE_NEW_CONSOLE,\ cwd = path) 

You can get rid of the os.getcwd call and the two os.chdir calls this way. If you want to use this script on Linux, you need to do without "cmd". So call Popen with ["jython", file] as the first argument.

EDIT: I just saw that CREATE_NEW_CONSOLE is not defined in the subprocess module when working on Linux. Use this:

 creationflags = getattr(subprocess,"CREATE_NEW_CONSOLE",0),\ 

This will be the same as before, except that it reverts to the default value of 0 when the subprocess module does not define CREATE_NEW_CONSOLE.

+1
source

All Articles