|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
||||
|
||||
|
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. |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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). |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
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.
|
|
#6
|
||||
|
||||
|
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... |
|
#7
|
||||
|
||||
|
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 |
|
#8
|
||||
|
||||
|
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. |
|
#9
|
||||
|
||||
|
ok, after a wee bit of research, i think i understand what the if __name__ == "__main__": line is all about...
|
|
#10
|
||||
|
||||
|
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. |
|
#11
|
||||
|
||||
|
oops,cut
Code:
import threading from the code ![]() |
|
#12
|
||||
|
||||
|
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. ![]() |
|
#13
|
||||
|
||||
|
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! |
|
#14
|
||||
|
||||
|
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 |
|
#15
|
|||
|