Comments (6)
If you've got a Popen object, the obvious way to send SIGINT is popen.send_signal(signal.SIGINT)
, which is a wrapper around the C kill()
function or similar (I'm only thinking about Unix-y platforms).
The direct equivalent of pressing Ctrl-C on a program in the terminal would be writing the single byte b'\x03'
to the 'master' side of the PTY, where the 'slave' side is connected to the process (I don't like these terms, but PTYs are still documented that way). Normally the kernel intercepts this and sends SIGINT to the foreground process in the terminal, but it's possible for the process to disable that and receive the byte as input instead. If you've created the pty (e.g. using this library), you already have a FD for the 'master' side; I don't know if it's possible to get the 'master' side from inside the terminal.
from ptyprocess.
Sending a signal directly should be independent of anything to do with the terminal. But it's common to handle (or even ignore) SIGINT. It looks like fzf is handling it here:
https://github.com/junegunn/fzf/blob/d18d92f925f791271b095df968d290ced05d065a/src/terminal.go#L2880
If this is meant to quit fzf, and you can't reproduce it with the UI visible, you could try using strace to see what it's doing when it gets the signal. My experience with strace is that you get a lot of output to make sense of, though.
from ptyprocess.
After a quick look, I think my answer would be that the most obvious ways to capture output from subprocesses should capture only stdout, not stderr. Capturing stderr should require some extra option, as in $(foo 2>1)
(in bash). I think this is basically the point of having two separate output streams, that 'normal' output capturing separates them out.
That's an answer about shell design, though, not really anything to do with PTYs. And I've never tried to design a shell, so don't place much weight on my thoughts. 🙂
from ptyprocess.
Thank you for your answer! I have no special question now so I'll just close this issue with impressing respect to you.
from ptyprocess.
@takluyver could you please take a look into the case I'm trying to crack.
There is popular fzf tool and owner told me how it works:
- Read input from STDIN
- Print UI to STDERR
- Wait for user input from
/dev/tty
👀 👀 👀 - Print to STDOUT
- Comment: "Normally programs read and write stdin/stdout to communicate with the user, but fzf uses trick: It opens
/dev/tty
which is always the terminal. This way fzf can be used in pipes e.g. 'find | fzf | xargs echo'."
In xonsh I see that during running the command (Popen) there is capturing stdin/stdout/stderr and command hangs but my question not about it.
Please advice how I can send SIGINT to this Popen-executed command if it reads /dev/tty
for user input? I don't understand how to do it and may be you give me the thread to follow.
Because xonsh uses PopenThread I can't just press Ctrl+C to send SIGINT to fzf. I can catch pressing Ctrl+C but after this I need to send it to the fzf process but I don't understand how to send it to fzf /dev/tty
.
Many thanks!
from ptyprocess.
I tried to manually do this with kill -s INT
but have no success:
!(fzf) # xonsh
pstree -s fzf
# pid=11 for xonsh, pid=22 for subprocess fzf
kill -s INT 22
# No reaction - fzf still in `pstree` without changes.
I think (hypothesis) this is because it is waiting input from /dev/tty
but because it ran in thread it lost connection to current tty...
from ptyprocess.
Related Issues (20)
- isalive(): check process status for non-child process HOT 13
- Incorrect distributive in WHEEL format HOT 1
- 0.5.2 not really released? HOT 1
- PtyProcess.spawn (and thus pexpect) is not safe for use in multithreaded applications HOT 6
- ptyprocess 0.5.1 and 0.5.2 appear to be missing the setup.py file. HOT 2
- Release? HOT 1
- is it possible to PtyProcess.read() without blocking? HOT 3
- PtyProcess.spawn (and thus pexpect) slowdown in close() loop HOT 2
- Rather than closing all the file descriptors, set close on exec on them HOT 3
- PtyProcess.spawn just got slower and can trigger problems HOT 2
- use os.posix_spawn when available (i.e, 3.8) HOT 4
- The preexec_fn should be executed before closing the file descriptors.
- Use stdin in child process HOT 12
- Release? HOT 1
- PtyProcess.read() returns a different value every call HOT 1
- pty cannot read unicode color escapde HOT 2
- The positions of the two arguments in setwinsize function appear to be reversed HOT 2
- test_spawn_sh occasionally fails
- ptyprocess.read() does not read last two lines
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 ptyprocess.