English 中文(简体)
关于从另一处打字
原标题:Regarding running a script from another
  • 时间:2010-12-02 09:23:00
  •  标签:
  • python
  • pygtk

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 out
print err

[EDIT]:此处的胎儿和胎儿分别处理产出和错误记录。

另外,我用什么植被来把产量集中在猪肉中?

最佳回答

次级处理模块是这一选择,但切碎部分是将产出与你的主要圆环相提并论,以实现这一目标,如果你身处,你可以轻易操作另一个透镜,并使用<条码>,即,在座标上使用read,但如果你计划把 application放在窗上,那么,你就应当使用<>斯特罗伯茨(。

关于植被,使用<代码>gtk。 文本Buffer associated with a gtk. 文本:<>。

这方面的一个例子是深read。

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()

载于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

注意,即便在更新的同时,纽芬兰仍然如何应对。

问题回答
import subprocess
pro = subprocess.Popen( abc.py )

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

您提到PyGtk,但您可以尝试PyQt<>,特别是QProces。 等级为:

  • readyReadStandardError
  • readyReadStandardOutput

Look for a similar tool with PyGtk.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...