2

I am looking for an Python3 script fragment that open new console window and comunicate with it.

Some scenario:

  • Open new console
  • Prompt "Input some data"
  • User write some data
  • Data is send from console back to Python script
  • Data is processed in script
  • Some information is send and displayed on earlier opened console.

I know how open new window:

os.system("gnome-terminal -e 'bash -c \"some instruction\"'")

but this knowledge is helpless for me, because I don't know how comunicate with this new console window.

Any help will be apreciate :)

BartekPL
  • 2,290
  • 1
  • 17
  • 34

2 Answers2

2

I'm a little unsure why you would want to do this. Can I suggest an entirely pythonic approach using threading and queues:

import threading
from queue import Queue
import time

def worker(q):
    """thread worker function"""
    running = True
    while running:
        message = q.get()
        print('Worker received message: {}'.format(message))
        if message == 'KILL':
            running = False
    print('Worker completed')

if __name__ == '__main__':
    q = Queue()
    worker = threading.Thread(target=worker, args=(q,))
    worker.start()
    running = True
    while running:
        user_input = input('Input some data: ')
        q.put(user_input)
        if user_input == 'KILL':
            running = False
        time.sleep(0.5)
    print('Program terminated')

You now have the ability to send commands to another python script from a console. The worker can do whatever you wish now. For a more detailed example see this post


Alternatively if thats not acceptable, what you are trying to do is 'PIPE' data from a command line in both directions. Have a look at this post where they do:

proc = subprocess.Popen(['some_cmd', '--command_line_parameters'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
F. Elliot
  • 228
  • 2
  • 10
  • I have multithread application, that logging in one console, and one of the thread is responsible for communication with user. This one thread should have new console window, because logs may previous one very unusable. – BartekPL Jun 13 '18 at 11:03
  • 1
    That makes more sense. I'd have a look at subprocess and popen. It may be worth following [this post](https://stackoverflow.com/questions/16768290/understanding-popen-communicate), and writing a lightweight python file for the purpose. That may make it easier to add certain things later on like command line parameters. I have done something similar in the past by creating a relatively simple socket server. Others have used [multiprocessing](https://docs.python.org/3.6/library/multiprocessing.html#managers) – F. Elliot Jun 13 '18 at 11:09
1

I've used a socket communication between two consoles.

It is some workaround, but this solution has all features I wanted in my program.

I've written second Python script that connect with my main program, and then get input from user and send it to main program.

BartekPL
  • 2,290
  • 1
  • 17
  • 34