python - Can I use fdpexpect on a file that's currently being written to? -


i'm trying wait until text written live logfile in python.

fdpexect seem right thing this, isn't waiting. hits end of file terminates.

i'm wondering if fdpexpect doesn't support , i'll need work around it?

the code have this:

creating spawn object:

# we're not using pexpect.spawn because want # output written logfile in real time,  # spawn doesn't seem support.  p = subprocess.popen(command,                      shell=shell,                      stdout=spawnedlog.getfileobj(),                      stderr=subprocess.stdout) # give fdspawn same file object gave popen return (p, pexpect.fdpexpect.fdspawn(spawnedlog.getfileobj())) 

waiting something:

pexpectobj.expect('something') 

this quits , before 'something' event happens eof error.

fdpexpect isn't design work on normal files. pexpect read file object until hits eof - pipes , sockets, won't happen until connection closed, normal files, happen entire file has been read. has no way of knowing file actively being written process.

you work around creating pipe using os.pipe, , implementing own tee functionality write stdout of process pipe in addition log file. here's little toy example seems work:

from subprocess import popen, pipe, stdout threading  import thread import os import pexpect.fdpexpect  # tee , teed_call based on http://stackoverflow.com/a/4985080/2073595  def tee(infile, *files):     """print `infile` `files` in separate thread."""     def fanout(infile, *files):         line in iter(infile.readline, ''):             f in files:                 f.write(line)         infile.close()     t = thread(target=fanout, args=(infile,)+files)     t.daemon = true     t.start()     return t  def teed_call(cmd_args, files, **kwargs):     p = popen(cmd_args,               stdout=pipe,               stderr=stdout,               **kwargs)     threads = []     threads.append(tee(p.stdout, *files))     return (threads, p)  open("log.txt", 'w') logf:     # create pipes unbuffered reading , writing     rpipe, wpipe = os.pipe()     rpipe = os.fdopen(rpipe, 'r', 0)     wpipe = os.fdopen(wpipe, 'w', 0)      # have pexpect read readable end of pipe     pobj = pexpect.fdpexpect.fdspawn(rpipe)      # call script, , tee output our log file ,     # writable end of pipe.     threads, p = teed_call(["./myscript.sh"], [wpipe, logf])      # myscript.sh print 'hey'     pobj.expect("hey")      # orderly shutdown/cleanup     t in threads: t.join()     p.wait()     rpipe.close()     wpipe.close() 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -