Giter Site home page Giter Site logo

Comments (5)

takluyver avatar takluyver commented on September 26, 2024

Reading data is a 'pull' operation, not a 'push' - the parent process has to explicitly call something to read data from the child. You can't reassign file descriptors of another process - e.g. to connect stdout directly to a file. You might want to spin out a separate thread (or use the asyncio event loop) to pull data from it and store it somewhere.

from pexpect.

jquast avatar jquast commented on September 26, 2024

Right, this is by design. Though you're not the first to ask, so it probably begs the need to document about this somewhere in the docs. I'm going to leave this open for such purpose.

I second @takluyver's recommendation -- spawn a thread that keeps calling readline() or read_nonblocking(). Your generate.sh will block at some later point when its stdout buffer is filled unless you begin to expect or read it. See #54 (comment)

If you wanted to redirect stdout to your main program, it could be as simple as:

class ContinueReadPrintLoop(threading.Thread):
    def __init__(self, child):
        self.child = child
        threading.Thread.__init__(self)

    def run(self):
        while True:
            try:
                print(self.child.readline().rstrip())
            except pexpect.EOF:
                print('EOF')
                break

child = pexpect.spawn('....')
child.expect(['this', 'and', 'that'])
ContinueReadPrintLoop(child).start()

(you would also need to use a shared threading.Lock instance acquire a lock to print anything to stdout if your main thread also prints, or if you run several of these children)

and yet, then what would you do? You wouldn't be able to interact with child anymore, is it OK if you don't know or care when or how it exits? Maybe what you really want is a background thread that just does child.expect(pexpect.EOF), at least this way, you can still be assured of the exit status of the child process.

from pexpect.

takluyver avatar takluyver commented on September 26, 2024

I suspect if we integrate this in Pexpect, it should also store it in self.buffer, in case you later want to expect something in it. Then there's some complexity about doing it with threading, because you need to ensure it stops before something else tries to read .buffer.

from pexpect.

takluyver avatar takluyver commented on September 26, 2024

Sorry, half asleep, I meant "if we integrate this in Pexpect"

from pexpect.

jquast avatar jquast commented on September 26, 2024

Marking as duplicate of #54. Otherwise submit a PR for a self-answering FAQ entry to reduce the misunderstanding of expect pull operation

from pexpect.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.