打开第二个Python控制台 [英] Open second Python console

查看:189
本文介绍了打开第二个Python控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法打开第二个python控制台,让新的控制台运行,而原来的控制台继续,当新的控制台完成它发送回其数据,以一个变量的形式回到原来的控制台? / p>

Is there a way to open a second python console and let the new console run while the original console keeps going and when the new console finishes it sends back its data, in the form of a variable back to the original console?

推荐答案

子流程模块可能是您要找的。然而,事情是,你得到之后整个程序完成后的过程输出。这意味着,如果你试图运行的程序永远运行,你将无法看到输出,直到它退出(通过强制它或使用终止方法)。

The sub-process module might be what you are looking for. However, the thing is, you get the output of the process after the entire program finishes. this means, if the program you are trying to run runs forever you will not be able to see the output until it is quit (either by forcing it or using termination methods).

如何将输出分配给变量的示例如下:

An example of how you would assign the output to a variable would be:

output,error=your_process.communicate()

输出部分是你将使用的(根据你的问题)。但是,错误是你得到如果你运行它,并有一个问题(不返回0)。如果你不想捕获错误,那么你可以简单地将它分配给 _

The output part of this is what you would be using (based on your question). However, the error is what you get if you run it and there is a problem (doesn't return 0). If you are not looking to capture errors then you can simply assign it to _.

正在使用关键字参数,我建议使用 shlex 库将您的字符串拆分为参数。 (你可以使用一个常规字符串,例如: var =mypythonprogram.py argument1 argument2并使用 arguments = shlex.split / code>,然后只需将其提供给子流程的参数。

Also note that if you are using key-word arguments, i would suggest using the shlex library for splitting your string into arguments. (you can just use a regular string such as: var="mypythonprogram.py argument1 argument2" and use arguments=shlex.split(var) and you can then just supply it into the arguments for the sub-process.

另一个选项,如果你不需要与程序交互将使用Threads,并有许多关于堆栈溢出的问题,以及大量的文档,官方,以及在互联网上的其他网站上。

Another option if you don't need to interact with the program would be using Threads, and there are many questions on stack overflow about them, as well as plenty of documentation both officially, and on other websites all over the internet.

这篇关于打开第二个Python控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆