Comments (5)
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.
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.
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.
Sorry, half asleep, I meant "if we integrate this in Pexpect"
from pexpect.
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)
- Support for new release HOT 3
- Two tests hang on `cat` HOT 3
- test_large_stdout_stream timeout HOT 1
- REPLWrapTestCase.test_existing_spawn fail on illumos HOT 1
- sdist is missing requirements-testing.txt
- AttributeError: module 'asyncio' has no attribute 'coroutine' HOT 1
- Incorrect DEVELOPERS.rst
- False positive expect_exact HOT 1
- Test REPLWrapTestCase.test_pager_as_cat fails.
- An asterisk appearing out of nowhere with Clojure
- Time for a release: any reason to delay? HOT 5
- 4.9.0: git tag does not match PyPI version HOT 2
- Handling SIGTSTP possible?
- Docs not updated for 4.9?
- 4.9: pytest fails in 3 units HOT 1
- "expect()" and "await expect()" have different results on completed processes
- Pexpect does not implement enough asynchronous methods to prevent the use of time.sleep().
- add contribution documentation
- Pxssh sometime does not capture the full output of previous `sendline`
- trouble using pexpect with julia
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pexpect.