python - Regarding running a script from another -
is there way can run script while getting output readily executing itself. example: use os.popen3 command execute abc.py, unable output abc.py readily doing python abc.py
; seems need wait os.popen3 command finish:
fin, fout, ferr=os.popen3("abc.py") out = fout.read() err = ferr.read() fo.write(out) fe.write(err) print out print err
[edit]:fo , fe here file handles output , error logs, respectively.
also, widget use populate output in, in pygtk?
the subprocess module option, tricky part follow output un parallel main loop of gtk, accomplish goal must have consider platform dealing, if in linux can run thread , use gtk.gdk.threads_init
use threads in pygtk, if planing run application on windows, should use generators , gobject.idle_add.
about widget, use gtk.textbuffer
associated gtk.textview
here example threads
import gtk import subprocess import threading gtk.gdk.threads_init() class followprocess(threading.thread): def __init__(self, cmd, text_buffer): self.tb = text_buffer self.child = subprocess.popen(cmd, shell=true, stdout=subprocess.pipe) super(followprocess, self).__init__() def run(self): while not self.child.poll(): out = self.child.stdout.read(1) if out != '': gtk.gdk.threads_enter() self.tb.insert_at_cursor(out) gtk.gdk.threads_leave() def destroy(w, cmd): cmd.child.terminate() gtk.main_quit() = 0 def click_count(btn): global message.set_text('calling button %d' %i) += 1 other_command = 'python extranger.py' w = gtk.window() w.resize(400, 400) message = gtk.label('nothing') tb = gtk.textbuffer() tv = gtk.textview(tb) scroll = gtk.scrolledwindow() scroll.set_policy(gtk.policy_automatic, gtk.policy_automatic) scroll.add(tv) box = gtk.vbox() button = gtk.button('active button') cmd = followprocess('python extranger.py', tb) button.connect('clicked', click_count ) w.connect('destroy', destroy, cmd) box.pack_start(button, false) box.pack_start(message, false) box.pack_start(scroll) w.add(box) w.show_all() cmd.start() gtk.main()
and in extranger.py
import time import sys = 0 while true: print 'some output %d' % sys.stdout.flush() # need see output time.sleep(.5) += 1
note how button stills responsive update in parallel.
Comments
Post a Comment