开发者

Regarding running a script from another

开发者 https://www.devze.com 2023-01-28 17:24 出处:网络
Is there a way that I can run a script from another while getting output as readily as I get by executing it by itself.

Is there a way that I can run a script from another while getting output as readily as I get by executing it by itself. For example: I use the os.popen3 command to execute abc.py, but I am unable to get output from abc.py as readily as I would with doing python abc.py; it seems that I need to wait for os.popen3 command to finish:

fin, fout, ferr=os.popen3("abc.py")
out = fout.read()
err = ferr.read()
fo.write(out)
fe.write(err)
print 开发者_Python百科out
print err

[EDIT]:fo and fe here are file handles to the output and error logs, respectively.

Also, what widget do I use to populate the output in, in pygtk?


import subprocess
pro = subprocess.Popen('abc.py')

Is a much better way of fiddling with another scripts ins, outs, and errors.


The subprocess module is the option, but the tricky part is to follow the output un parallel with your main loop of gtk, to accomplish that goal you must have to consider the platform that you are dealing, if you are in linux you can easily run another thread and use gtk.gdk.threads_init to use threads in pygtk, but if you are planing to run your application on windows, then you should use generators and gobject.idle_add.

About the widget, use gtk.TextBuffer associated with a gtk.TextView

Here is an example with 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()

i = 0
def click_count(btn):
    global i
    message.set_text('Calling button %d' %i)
    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

i = 0
while True:
    print 'some output %d' % i
    sys.stdout.flush() # you need this to see the output
    time.sleep(.5)
    i += 1

Note how the button stills responsive even with the update in parallel.


You mentioned PyGtk but you can give a try to PyQt and particularly QProcess class wich has some nice signals like :

  • readyReadStandardError
  • readyReadStandardOutput

Look for a similar tool with PyGtk.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号