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?
-
Yes, but the how depends on what you want to use this for.Fred Foo– Fred Foo2012年07月23日 23:29:56 +00:00Commented Jul 23, 2012 at 23:29
-
1You can do it, but it seems convoluted. What are you tring to do? Could you elaborate?jfs– jfs2012年07月23日 23:34:08 +00:00Commented Jul 23, 2012 at 23:34
5 Answers 5
Perhaps you should look into the multiprocessing or multi-threading modules. These help you spawn off child processes from your original (parent) program.
Comments
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()
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 _.
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.
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.
5 Comments
multiprocessing module is not the solution. .communicate() is a method of Popen class from subprocess module. There are not enough details to provide a reasonable answer to the question in its current form.Read up on Python multiprocessing. It has examples of exchanging objects between processes.
Comments
It sounds like you want to do something like what the multiprocessing library offers. With out more info all i can do is point you to the docs.
for instance:
http://www.ibm.com/developerworks/aix/library/au-multiprocessing/ or http://docs.python.org/library/multiprocessing.html
Comments
If you are on windows you can use win32console module to open a second console for your thread or subprocess output
Here is a sample code:
import win32console
import multiprocessing
def subprocess(queue):
win32console.FreeConsole() #Frees subprocess from using main console
win32console.AllocConsole() #Creates new console and all input and output of subprocess goes to this new console
while True:
print(queue.get())
#prints any output produced by main script passed to subprocess using queue
if __name__ == "__main__":
queue = multiprocessing.Queue()
multiprocessing.Process(target=subprocess, args=[queue]).start()
while True:
print("Hello World")
queue.put("Hello to subprocess console")
#sends above string to subprocess which prints it into its own console
#and whatever else you want to do in ur main process
You can also do this with threading. You have to use queue module if you want the queue functionality as threading module doesn't have queue
Here is the win32console module documentation