asynchronous - in gobject's spawn_async, why doesn't my child process appear to see data until its stdin pipe is full -
i use following calls spawn process, file descriptors stdin, stdout, , stderr, , watch output on stdout:
[widget.pid,widget.stdin,widget.stdout,widget.stderr] = \ gobject.spawn_async(['/bin/sed','s/hi/by/g'], standard_input=true, standard_output=true, standard_error=true ) gobject.io_add_watch(widget.stdout, gobject.io_in, getdata) i write lines widget.stdin, expecting trigger callback function getdata.
what find getdata callback gets called once pipe widget.stdin has been filled, rather send first newline. complete test program here, along output. don't calls getdata until pipe fills or until close pipe, sed's returned data:
#!/usr/bin/env python # example base.py import pygtk pygtk.require('2.0') import gtk import gobject import os import time import fcntl f_setpipe_sz = 1031 # linux 2.6.35+ f_getpipe_sz = 1032 # linux 2.6.35+ def data_can_be_sent(source,cb_condition): msg = "hi there how you\n" try: print "source, cond, send", source, cb_condition, msg #print "also sending padding x's , newline make 512 bytes." os.write(source, msg) os.write(source,"\n") os.write(source,"-"*(510-len(msg))) os.write(source,"\n") time.sleep(0.1) except exception e: print e print "could not print in data_can_be_sent" return true def getdata(source,cb_condition): try: p = os.read(source,4096) print "source, cond, get", base.pipesize, source, cb_condition, "[", p, "]" except: print "could not print in getdata." return true class base: pipesize = 0 def hello(self, widget, data=none): print "hello" print gobject try: print "fstat",os.fstat(widget.stdin) print "fsync" os.fsync(widget.stdin) except attributeerror e: print "attribute error",e print "spawning sed process" [widget.pid,widget.stdin,widget.stdout,widget.stderr] = \ gobject.spawn_async(['/bin/sed','s/hi/by/g'], standard_input=true, standard_output=true, standard_error=true ) print widget.pid,widget.stdin,widget.stdout,widget.stderr print "pipe size",fcntl.fcntl(widget.stdin,f_getpipe_sz) print "set pipe size",fcntl.fcntl(widget.stdin,f_setpipe_sz,10) base.pipesize = fcntl.fcntl(widget.stdin,f_getpipe_sz) print "pipe size",fcntl.fcntl(widget.stdin,f_getpipe_sz) gobject.io_add_watch(widget.stdin, gobject.io_out, data_can_be_sent) gobject.io_add_watch(widget.stdout, gobject.io_in | gobject.io_pri, getdata) os.write(widget.stdin, "aaaaaaaaaaaaaaa\n"*64) os.write(widget.stdin, "bbbbbbbbbbbbbbb\n"*64) os.write(widget.stdin, "ccccccccccccccc\n"*64) os.write(widget.stdin, "ddddddddddddddd\n"*64) except oserror e: print "nonattrib",e.errno print "nonattrib",e.strerror def delete_event(self, width, event, data=none): print "delete event, returning false window destroyed" return false def destroy(self, widget, data=none): print "destroy received" gtk.main_quit() def __init__(self): self.window = gtk.window(gtk.window_toplevel) self.window.connect("delete_event", self.delete_event) # here connect "destroy" event signal handler. # event occurs when call gtk_widget_destroy() on window, # or if return false in "delete_event" callback. self.window.connect("destroy", self.destroy) self.button = gtk.button("hello world") self.button.connect("clicked", self.hello, none) self.window.add(self.button) self.button.show() self.window.show() def main(self): gtk.main() print __name__ if __name__ == "__main__": base = base() base.main() the output looks this:
$ python pytest.py __main__ hello <module 'gobject' '/usr/lib/python2.7/dist-packages/gobject/__init__.pyc'> fstat attribute error 'gtk.button' object has no attribute 'stdin' spawning sed process 29594 16 17 19 pipe size 65536 set pipe size 4096 pipe size 4096 source, cond, send 16 4 hi there how source, cond, send 16 4 hi there how source, cond, 4096 17 1 [ aaaaaaaaaaaaaaa (more a's, b's, c's, , d's)... ddddddddddddddd ] source, cond, send 16 4 hi there how source, cond, send 16 4 hi there how source, cond, send 16 4 hi there how source, cond, send 16 4 hi there how source, cond, send 16 4 hi there how source, cond, send 16 4 hi there how source, cond, send 16 4 hi there how source, cond, send 16 4 hi there how source, cond, 4096 17 1 [ there how --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- there how --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- there how --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- there how --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- there how --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- there how --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- there how --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- there how --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ] source, cond, send 16 4 hi there how source, cond, send 16 4 hi there how source, cond, send 16 4 hi there how source, cond, send 16 4 hi there how source, cond, send 16 4 hi there how source, cond, send 16 4 hi there how source, cond, send 16 4 hi there how delete event, returning false window destroyed destroy received source, cond, send 16 4 hi there how source, cond, 4096 17 1 [ there how --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- there how --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- there how --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- there how --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- there how --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- there how --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- there how --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- there how -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ] update: in response drahnr's comment below, tried using gtk in c, call
ret = g_spawn_async_with_pipes(".", /* working directory */ spawn_argv, /* gchar **argv,*/ null, /* gchar **envp,*/ 0l, /* gspawnflags flags,*/ null, /* gspawnchildsetupfunc child_setup,*/ null, /* gpointer user_data,*/ &pid, /*gpid *child_pid,*/ &spawn_stdin, /* gint *standard_input,*/ &spawn_stdout, /* gint *standard_output, */ &spawn_stderr, /* gint *standard_error, */ null /*gerror **error*/ ); and got same results; no return sed until after @ least 4096 bytes had been sent sed, though there newline every ten chars.
Comments
Post a Comment