Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 12th, 2004, 10:13 PM
ktpr ktpr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 17 ktpr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 47 sec
Reputation Power: 0
why is my fileno -1?

we have lis.py, which does this
Code:
import sys

print sys.stdin.fileno()
print sys.stdout.fileno()

and we have the following
Code:
import os

pin, pout = os.popen2("lis.py", 't', -1)

for line in pout.readlines():
    print line

pin.close()
pout.close()


If you run lis.py by itself, you got 0 and 1 (for stdin and out, respectively). But when I create a pipe to lis.py, we get -1 and 1 (for its stdin and stdout). A fileno of -1 is invalid.

Why is the new stdin -1??? I wish to talk to lis.py and read the result, in order to verify IPC done with popen.

Reply With Quote
  #2  
Old December 13th, 2004, 02:46 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 10 m 32 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
I did a few quick tests on the code but ended up with the expected 0 and 1 each time. Here's the session, hopefully you'll be able to spot something I didn't .

Also note that you don't have to pass the second and third arguments to popen2().

Code:
>>> stdi, stdo = os.popen2('/home/Netytan/Desktop/lis.py', 't', -1)
>>> stdi
<open file '<fdopen>', mode 'w' at 0x40281650>
>>> stdo
<open file '<fdopen>', mode 'r' at 0x40281698>
>>> stdo.read()
'0\n1\n'
>>> stdi, stdo = os.popen2('/home/Netytan/Desktop/lis.py')
>>> for line in stdo:
...     print line,
...
0
1
>>>


If your using Python 2.4 for this you might also be interested in the subprocess module [recommended]. What version are you using anyway?

http://www.python.org/doc/2.4/lib/module-subprocess.html

Have fun,

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old December 13th, 2004, 04:59 PM
ktpr ktpr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 17 ktpr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 47 sec
Reputation Power: 0
netytan - Are you using linux to run this example? I've verified that this works on another linux machine.

It was also suggested that since .py aren't really executables in the sense that popen expects that might be why popen returns an invalid stdin. doing something like C:\...\python lis.py doesn't work either. I'm not sure what the problem is. I'm using w2k with py2.3

Reply With Quote
  #4  
Old December 14th, 2004, 04:16 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 10 m 32 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
You’re right; I ran the example on Susu 9.1 so that would explain it. It could just be a glitch in the way popen2() works on Windows but I'd lean away from that assumption for now. Anyway I'll give it a go tonight.

Until then you could try doing 'python C:\...\lis.py' -- assuming that python is on your PATH variable (or you need to do 'C:\Python23\python C:\...\lis.py'.)

Let me know if it works,

Mark.

Reply With Quote
  #5  
Old December 14th, 2004, 02:33 PM
ktpr ktpr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 17 ktpr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 47 sec
Reputation Power: 0
Ah... it works! It goes like this:

cmd3 = 'python lis.py'

and from there, using cmd3, I get a valid pipe to stdin. Thanks a lot.

cheers
ktpr

Reply With Quote
  #6  
Old December 15th, 2004, 04:12 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 10 m 32 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Glad you got it working , anyway here's my test session on Windows XP for those of you who are interested.

Code:
>>> stdi, stdo = os.popen2('python "C:\Documents and Settings\Mark\Desktop\lis.py"')
>>> stdo.read()
'0\n1\n'
>>> 


As you can see It seems to work as it does on *nix assuming that you invoke python in just the right way or you get nothing useful .

Mark.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > why is my fileno -1?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT