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:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old February 9th, 2004, 01:37 AM
macaronikazoo's Avatar
macaronikazoo macaronikazoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: brisbane - australia
Posts: 10 macaronikazoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
running programs from python

i'm trying to write a python script that will be able to run programs on the local machine. the problem is that everytime i start another program, python gets killed.

it says in the manual that the current process gets replaced when using:

os.execvp("render", ["bleh","d:\\test.mb"])

and that it doesn't return. is there any way to start a separate process from python, and monitor it in some way?

thanks for any help.

Reply With Quote
  #2  
Old February 9th, 2004, 04:26 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,529 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 17 h 19 m 5 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 take it you're a mac user judging by your name. You should try the os.system() function however i don't know the command you need. On windows and *nix you just point at the file i.e.

Code:
>>> import os
>>> os.system('file.py')


it's as simple as that. Or if you need to capture output from the command line you can use os.popen().read()

Code:
>>> os.popen('echo Python').read()
'Python\n'
>>>


None of these should stop the current process so , you could also use threads to run things in a sub process...

http://www.python.org/doc/2.3.3/lib/module-thread.html

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


Reply With Quote
  #3  
Old February 9th, 2004, 08:46 AM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
I doubt the name really signifies he's a Mac user ...

Anyway, a bit more description as to what you are trying to accomplish might help us figure out which method of executing things from Python you should use (there are many).

Reply With Quote
  #4  
Old February 9th, 2004, 04:20 PM
macaronikazoo's Avatar
macaronikazoo macaronikazoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: brisbane - australia
Posts: 10 macaronikazoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
oh wow cool. seems to be just what i was after.

second question, is there some way i can get python to "do something" whenever the process spits out new info? or do i have to check it at set intervals?

basically i'm looking at writing a client/server script, where the server python script tells a client python script to kick off a process. when that process finishes, i want the client to report back to the server...

PS i'm on windows, not mac...

Last edited by macaronikazoo : February 9th, 2004 at 04:35 PM.

Reply With Quote
  #5  
Old February 9th, 2004, 04:55 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
You can use one of the many popen variants in Python and then select() on whatever file descriptor you want (stdout, stderr). Then when there's data to be read, act accordingly.

Reply With Quote
  #6  
Old February 9th, 2004, 05:39 PM
macaronikazoo's Avatar
macaronikazoo macaronikazoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: brisbane - australia
Posts: 10 macaronikazoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
hmmmm, ok i think i'm getting there. i'm just not 100%sure on how to use the select function. doing this:

import os
render=os.opopen("render d:\\test.mb")
select([],[render],[])

gives me an error that its not a socket. it says something about only working on sockets on windows. is there a work around? or am i going to have to set up something to check the render object every couple of seconds?

also is there any way to tell if a process is finished? because when i do this:

go=os.opopen("render -help")

go.readlines() gives me the help string the render command spits out normally. but once the command spits that out, it obvisouly exits. there doesn't seem to be any way to ask the go object if its process has finished...

Reply With Quote
  #7  
Old February 9th, 2004, 07:38 PM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
Unfortunately if it's a Windows OS then select only supports sockets and not file descriptors
Alternatively, you can use a new thread for the popen command to avoid blocking the main code then pass the command results back. This sample should work on Windows:
Code:
import popen2
import time
result = '!' 
running = False

class pinger(threading.Thread):
    def __init__(self,num,who):
        self.num = num
        self.who = who
        threading.Thread.__init__(self)

    def run(self):
        global result
        cmd = "ping -n %s %s"%(self.num,self.who)
        fin,fout = popen2.popen4(cmd)
        while running:
            result = fin.readline()
            if not result:
                break
        fin.close()
        
if __name__ == "__main__":
    running = True
    ping = pinger(5,"127.0.0.1")
    ping.start()
    now = time.time()
    end = now+300
    old = result
    while True:
        if result != old:
            print result.strip()
            old = result
        if time.time() > end:
            print "Timeout"
            running = False
            break
        if not result:
            print "Finished"
            break

Because one thread reads and the other thread sets the result there is no need to use locks.
Grim

Reply With Quote
  #8  
Old February 9th, 2004, 08:53 PM
macaronikazoo's Avatar
macaronikazoo macaronikazoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: brisbane - australia
Posts: 10 macaronikazoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
wow, cool. that looks like heaps of stuff for me to get through. i'm pretty new to all of this if you hadn't figured.

with that said, a bit of a noob question coming up. what does the line:

if __name__ == "__main__":

do exactly? presumably if all the above code is in a module of its own, this has something to do with calling the module itself, but i'm not quite sure i understand it properly.

thanks for all the help guys, it is indeed appreciated.

Reply With Quote
  #9  
Old February 9th, 2004, 09:41 PM
macaronikazoo's Avatar
macaronikazoo macaronikazoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: brisbane - australia
Posts: 10 macaronikazoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ok, after a wee bit of research, i think i understand what the if __name__ == "__main__": line is all about...

Reply With Quote
  #10  
Old February 10th, 2004, 06:38 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,529 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 17 h 19 m 5 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
Just to make sure - since this line is very comon - all it actually does is check if the program is being run (__name__ is '__main__') and not imported.

This way anything in the if statment will only be run when the program is run

That what you had?

Mark.

Last edited by netytan : February 10th, 2004 at 06:45 AM.

Reply With Quote
  #11  
Old February 10th, 2004, 06:59 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
oops,cut
Code:
import threading

from the code

Reply With Quote
  #12  
Old February 10th, 2004, 03:49 PM
macaronikazoo's Avatar
macaronikazoo macaronikazoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: brisbane - australia
Posts: 10 macaronikazoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
netytan: yeah, thats pretty much what I had. somewhat surprisingly, if you do a search for

if __name__ == "__main__":

in google, you get heaps of really useful results. yay for google.

grim: yeah thanks, i figured that out in the end.

Reply With Quote
  #13  
Old February 11th, 2004, 06:52 PM
macaronikazoo's Avatar
macaronikazoo macaronikazoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: brisbane - australia
Posts: 10 macaronikazoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ok, grim, your code works fine, and i'm pretty sure i understand the vast majority of it, but i'm having a bit of trouble with part of it.

where you've got:
fin, fout=popen2.popen4(cmd)

firstly, should it be:
fout,fin=popen2.popen4(cmd)

instead?? but anyway, looking in the docs, it says that instances of popen4 have the following method: wait( ) and also the attribute: pid

now, i don't quite understand, because popen4 returns a tuple - which is assigned to the objects fin and fout...

so what is the instance of popen4? if i do this instead:

job=popen2.popen4(cmd)

so if you didn't know popen4 returned a tuple, you'd think job was an instance of popen4. but trying to access job.pid gives me an error that the attribute doesn't exist. and using the method wait() also doesn't work...

i'm getting real confused by it all. how can i get (using grims example) a pinger instance to do something when the job finishes? i'd prefer to not have to use global variables like he has. I just want to be able to call a method of a pinger object, that kicks off the process, and then does something when that process finishes.

cheers!

Reply With Quote
  #14  
Old February 12th, 2004, 02:52 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
Yep, module popen2 functions return the fin,fout in the reverse order to the os module

Yep, confusing naming used
popen2.Popen4() is the class
popen2.popen4() is the function

If you are a Windows user the class is not available
I suggest the work around is to call a batch file instead of the .exe. You can then test the return values of the .exe and emit some text as a result. You can just monitor the batch file for one success/fail string.

My code just shows the working principles. Don't use globals if don't want to. You can always define the threading class within a function

Cheers,
Grim

Reply With Quote
  #15  
Old February 12th, 2004, 08:05 AM
macaronikazoo's Avatar
macaronikazoo macaronikazoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)