LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2014, 04:26 PM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 70
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?

Last edited by trist007; 03-29-2014 at 04:43 PM.
 
Old 03-29-2014, 05:23 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 03-29-2014, 07:17 PM   #3
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
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.
 
Old 03-29-2014, 08:42 PM   #4
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
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)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] terminal control Ribo01 Linux - Newbie 13 02-09-2011 02:55 PM
[SOLVED] Python on terminal dexter.gdv Programming 20 10-13-2010 07:14 AM
transfering control to a terminal? shamgar03 Linux - General 2 08-10-2006 11:29 AM
Python Terminal Gui Kedelfor Programming 2 10-24-2005 03:50 PM
Basic Linux Programming Question (QT terminal control) Boffy Programming 5 09-09-2004 12:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration