简体   繁体   English

使用子进程模块从python启动jython程序?

[英]Starting jython program from python using subprocess module?

I have a jython server script (called rajant_server.py) that interacts with a java api file to communicate over special network radios. 我有一个jython服务器脚本(称为rajant_server.py),它与java api文件交互,通过特殊的网络无线电进行通信。 I have a python program which acts as a client (and does several other things as well). 我有一个python程序充当客户端(并做其他几件事)。 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

Once the server successfully connects it waits for the client, which I start by running: 一旦服务器成功连接,它就会等待客户端,我开始运行它:

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

When the client connects, the server prints out information (currently for debug) in it's command/terminal window, and the client program prints out debug information in it's command/terminal window. 当客户端连接时,服务器在其命令/终端窗口中打印出信息(当前用于调试),客户端程序在其命令/终端窗口中打印出调试信息。 What I want to do is do away with the complex process by calling jython from my 'main.py' program using the subprocess module. 我想要做的是通过使用子进程模块从我的'main.py'程序调用jython来消除复杂的过程。

The problem is two fold: 问题有两个:

1 - I need the rajant_server.py program to open in it's own terminal/command window 1 - 我需要rajant_server.py程序在它自己的终端/命令窗口中打开

2 - jython needs to be run in the directory where the rajant_server.py file is stored, in other words, typing the following into the command/Terminal Window doesn't work (don't ask me why): 2 - jython需要在存储rajant_server.py文件的目录中运行,换句话说,在命令/终端窗口中键入以下内容不起作用(不要问我原因):

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

but: 但:

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

does work. 确实有效。


Okay... I just got something to work. 好的......我只是有点工作了。 It seems like a bit of a hack, so I would still love ideas on a better approach. 这看起来有点像黑客,所以我仍然喜欢更好的方法。 Here is what I am currently doing: 这是我目前正在做的事情:

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 in both linux & windows you would be my hero. 如果你有一个可以在linux和windows中运行的解决方案,你将成为我的英雄。 For some reason I couldn't change the stdin or stdout specifications on the subprocess when I added creationflags = subprocess.CREATE_NEW_CONSOLE. 出于某种原因,当我添加creationflags = subprocess.CREATE_NEW_CONSOLE时,我无法更改子进程上的stdin或stdout规范。

The Popen function in subprocess accepts an optional parameter 'cwd', to define the current working directory of the child process. 子进程中的Popen函数接受可选参数'cwd',以定义子进程的当前工作目录。

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. 你可以通过这种方式去除os.getcwd调用和两个os.chdir调用。 If you want to be able to use this script on Linux, you have to do without 'cmd'. 如果您希望能够在Linux上使用此脚本,则必须不使用“cmd”。 So call Popen with ["jython", file] as first argument. 所以用["jython", file]作为第一个参数调用Popen。

EDIT: I've just seen that CREATE_NEW_CONSOLE is not defined in the subprocess module when running on Linux. 编辑:我刚刚看到在Linux上运行时,子进程模块中没有定义CREATE_NEW_CONSOLE。 Use this: 用这个:

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

This will be the same as before, except it falls back to the default value 0 when the subprocess module does not define CREATE_NEW_CONSOLE. 这将与之前相同,只是当子进程模块未定义CREATE_NEW_CONSOLE时,它将回退到默认值0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM