简体   繁体   English

如何在命令提示符下将 python 变量传递给进程

[英]How can I pass a python variable to a process in the command prompt

I have a python loop that at each iteration is creating a new image in a new path.我有一个 python 循环,每次迭代都会在新路径中创建一个新图像。 I want to send that path into a process previously executed that is waiting for a path at the command prompt.我想将该路径发送到先前执行的进程中,该进程正在命令提示符处等待路径。

In more detail:更详细地:

I am running a self-driving simulator in python and every 5 frames I want to test that current frame (that is saved in a RAM disk) into an object detector algorithm I have trained (that spends arround 9ms to detect my object BUT 2 seconds to open the process).我在 python 中运行一个自动驾驶模拟器,每 5 帧我想将当前帧(保存在 RAM 磁盘中)测试到我训练过的对象检测器算法中(花费大约 9 毫秒来检测我的对象,但需要 2 秒)打开进程)。 Actually, I execute the trained algorithm by using the subprocess module, but the problem I have is that when that process is opened (I just open the process once, when I run the main script) it is waiting for a path image.实际上,我通过使用 subprocess 模块来执行经过训练的算法,但我遇到的问题是当该进程打开时(我只打开一次进程,当我运行主脚本时)它正在等待路径图像。 I believe that with your tips I am close to the answer but I don't face how to pass that path image to this subprocess that is waiting for it at each 5 frames iteration.我相信根据您的提示,我已经接近答案了,但我不知道如何将该路径图像传递给这个在每 5 帧迭代时等待它的子进程。

PD: I am on Windows, Python 3.5.4 PD:我在 Windows 上,Python 3.5.4

Do you know what can I do?你知道我能做什么吗?

If you're on a *nix environment, and I'm understanding what you want, pipes provides what you want to do quite elegantly.如果您在 *nix 环境中,并且我了解您想要什么,管道会非常优雅地提供您想要做的事情。 I don't know Windows, but maybe the same concept could be used there.我不知道 Windows,但也许可以在那里使用相同的概念。

Here's a simple example that illustrates this:这是一个简单的例子,说明了这一点:

Pipe1a.py:管道1a.py:

#!/usr/bin/env python

import time
import sys

for i in range(10):
    time.sleep(1) # represent some processing delay
    print("filename{}.jpg".format(i))
    sys.stdout.flush()

Pipe1b.py:管道1b.py:

#!/usr/bin/env python

import sys

while True:
    line = sys.stdin.readline()
    if len(line) == 0:
        break
    print "Processing image '{}'".format(line.strip())

If you made both of these scripts executable, then you could chain them together via a pipe at the command prompt:如果您使这两个脚本都可执行,那么您可以在命令提示符下通过管道将它们链接在一起:

> Pipe1a.py | Pipe1b.py

Resulting output:结果输出:

Processing image 'filename0.jpg'
Processing image 'filename1.jpg'
Processing image 'filename2.jpg'
Processing image 'filename3.jpg'
Processing image 'filename4.jpg'
Processing image 'filename5.jpg'
Processing image 'filename6.jpg'
Processing image 'filename7.jpg'
Processing image 'filename8.jpg'
Processing image 'filename9.jpg'

The concept here is that one process writes data to its stdout and a second process reads that data from its stdin .这里的概念是一个进程将数据写入其stdout ,第二个进程从其stdin读取该数据。

If you don't want to use the command prompt to string these together, but rather want to run a single script, you can do this same thing, with pretty much the same code, using the Python subprocess1 module.如果您不想使用命令提示符将它们串在一起,而是想运行单个脚本,则可以使用 Python subprocess1模块使用几乎相同的代码执行相同的操作。 For this example, you could have the Pipe1b.py program run the Pipe1a.py program via subprocess.Popen and then process its output in this same way.在这个例子中,你可以有Pipe1b.py程序运行Pipe1a.py通过程序subprocess.Popen ,然后再处理它以同样的方式输出。

Thank you for all your comments and for your help.感谢您的所有评论和帮助。 Finally I have achieved it.我终于实现了。

I use the library 'pexpect' that allows to launch a program inside a python script with the function process = popen_spawn.PopenSpawn .我使用库“pexpect”,它允许在 python 脚本中使用函数process = popen_spawn.PopenSpawn启动程序。 Then, a function from that library called 'send' allows to pass an argument to that running process ( process.send(arg) ).然后,该库中名为“send”的函数允许将参数传递给该正在运行的进程( process.send(arg) )。

In my case, I launch the program (.exe) at the beginning of the script defining that instance as a global variable.就我而言,我在脚本的开头启动了程序 (.exe),将该实例定义为全局变量。 Then I just have to execute the send function at each iteration.然后我只需要在每次迭代时执行send函数。

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

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