LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   A question about terminal control in Python... (https://www.linuxquestions.org/questions/programming-9/a-question-about-terminal-control-in-python-4175499906/)

trist007 03-29-2014 04:26 PM

A question about terminal control in Python...
 
This is a wrapper program that uses the xmp binary to play mod files at random. However, I would like to have another process running so that if a good song is playing I can write the name of the filename to a file for future reference.

Code:

#!/usr/bin/python
# -*- coding:utf-8 -*-

"""
wrapper for xmp
to play random files
"""

import random
import os
import sys


def main():
        files = os.listdir('/Users/rgonzale/Mods')
        command = "/usr/local/bin/xmp /Users/rgonzale/Mods/%s" % (files[random.randint(0,(len(files) - 1))])

        pid = os.fork();
        if pid > 0:
                os.system(command);
        if pid == 0:
                c = sys.stdin.read(1);
                if c == 'e':
                        print "hope this works"

if __name__ == "__main__":
        main()

When the parent runs the command it has stdin, stdout, and stderr to the pty that I ran the process from. The character 'e' is not a character that xmp will do anything with from stdin, I checked in the man page. When I have the child try and read a char from stdin, I can't get it to print "hope this works".

At the point where the os.fork() occurs, do both processes have stdin, stdout, and stderr all tied to the pty I am in? When it is forked, I understand that the child has an exact copy of all the file descriptors which should include stdin, stdout, and stderr that are similar to those of the parent.

I think I need to be using the termios module. However, the documentation for the Python termios module is slim to none. It is also a little hard to grasp. Could somebody give me an example of how I would use that module in my specific case?

jlinkels 03-29-2014 05:23 PM

I have no experience with Python forking, but right after the fork, both processes share the same pty. So to debug, write this piece in your code:

Code:

pid = os.fork();
        if pid > 0:
                print "this is the parent"
        if pid == 0:
                print "this is the child"

Both sentences should appear on screen. Which appears first is undetermined.
Once you see this, it is easier to take it from there.

jlinkels

trist007 03-29-2014 07:17 PM

Ah ok thank you. Turns out I just needed to flush stdout continuously so that the output of the xmp process that the child ran continues to go to the screen.

trist007 03-29-2014 08:42 PM

I got what I was looking for.

1. Continuously flush stdout so that the output of the child process can continuously be outputted to the terminal.

2. non-blocking console input so that I can add a filename of a song to a favorites file.

Code:

def isData():
        return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], [])

old_settings = termios.tcgetattr(sys.stdin)
try:
        tty.setcbreak(sys.stdin.fileno())
        i = 0
        while 1:
                print i
                i += 1

                if isData():
                        c = sys.stdin.read(1)
                        if c == '\x1b':        # x1b is ESC
                                break

finally:
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)



All times are GMT -5. The time now is 12:05 PM.