1

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?

jfs
417k211 gold badges1k silver badges1.7k bronze badges
asked Jul 23, 2012 at 23:26
2
  • Yes, but the how depends on what you want to use this for. Commented Jul 23, 2012 at 23:29
  • 1
    You can do it, but it seems convoluted. What are you tring to do? Could you elaborate? Commented Jul 23, 2012 at 23:34

5 Answers 5

2

Perhaps you should look into the multiprocessing or multi-threading modules. These help you spawn off child processes from your original (parent) program.

answered May 28, 2013 at 20:03
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Jul 23, 2012 at 23:40

5 Comments

-1: if by "python console" the OP means Python REPL then 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.
@J.F.Sebastian: Was that worth downvoting all 3 answers, after the OP has accepted an answer, just because the question is ambiguous?
@J.F.Sebastian: does seem kind of bad that they get blamed for my vagueness btw can you please explain what a "REPL" is i've never heard the term.
@Scotty: "get the output of the process after the entire program finishes" so by entire do u mean both the original and the new console.
0

Read up on Python multiprocessing. It has examples of exchanging objects between processes.

answered Jul 23, 2012 at 23:31

Comments

0

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

answered Jul 23, 2012 at 23:32

Comments

0

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

answered Mar 31, 2021 at 9:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.