Sending SIGINT to process launched from Python does not yield same result as when process is launched from...












0














I am writing a script that is supposed to launch an interactive shell of a third-party program from within python in linux (the third party program is Stata). The purpose of the script is to control the stdin and stdout of the interactive shell so I can access it from an editor and other scripts.



When started from within a linux shell, Stata launches an interactive shell where you can run Stata commands. When I press control+C or use some other way to send a SIGINT signal to Stata, it will stop executing the current command and will return to the interactive shell, but it will not kill the Stata process. I want to replicate this behavior for Stata launched from python.



When I send a SIGINT signal to a Stata process launched from python, it will kill the process instead of just stopping execution of the current command.



The behavior in the shell is:



> /usr/local/stata14/stata
(...other stuff...)
. forval i = 1/100000 {
2. sleep 10
3. di "`i'"
4. }
1
2
3
4
(I hit CTRL+C)
--Break--
r(1);
.


The following python program launches Stata from within python:



def handler(signum, frame):
stata.send_signal(signal.SIGINT)
signal.signal(signal.SIGINT, handler)

stata = subprocess.Popen(["/usr/local/stata14/stata"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setpgrp)

(... other stuff hat handles forwarding stata.stdout to sys.stdout in a separate thread...)

while true:
time.sleep(0.1)
userinput = raw_input(". ")
print(userinput, file=stata.stdin)
stata.stdin.flush()
print(stata.poll())


with the same for loop running in the subprocess, produces:



> /usr/local/stata14/stata
. forval i = 1/100000 {
2. sleep 10
3. di "`i'"
4. }
1
2
3
4
-2
-2
-2


and any further attempt to write to stata.stdin then produces



print(userinput, file=stata.stdin)
IOError: [Errno 32] Broken pipe


Anyone understands why this is happening? I already tried using subprocess.Popen with shell = True. In this case, Popen launches an sh process and a Stata process. If I send SIGINT to the sh process, nothing happens. If I send it to Stata, the same problem as with shell=False arises.



Thanks in advance.










share|improve this question






















  • If you use kill -int instead to simulate what your doing from Python, you'll see the same behavior. Ctrl+C will send sigint to the entire foreground process group.
    – that other guy
    Nov 19 '18 at 12:36










  • I put the subprocess in a different group using preexec_fn=os.setpgrp (hard to spot because the code above does not wrap lines). But it's true that I do observe the same behavior when I use kill -int on the process launched from python. Anyways: how can I replicate the behavior of the process launched without python for the process launched in python?
    – Tobias
    Nov 19 '18 at 12:44












  • Apparently the underlying program just acts differently when it is run in a terminal. So I solved this using pty.fork() instead of subprocess.
    – Tobias
    Nov 20 '18 at 20:50
















0














I am writing a script that is supposed to launch an interactive shell of a third-party program from within python in linux (the third party program is Stata). The purpose of the script is to control the stdin and stdout of the interactive shell so I can access it from an editor and other scripts.



When started from within a linux shell, Stata launches an interactive shell where you can run Stata commands. When I press control+C or use some other way to send a SIGINT signal to Stata, it will stop executing the current command and will return to the interactive shell, but it will not kill the Stata process. I want to replicate this behavior for Stata launched from python.



When I send a SIGINT signal to a Stata process launched from python, it will kill the process instead of just stopping execution of the current command.



The behavior in the shell is:



> /usr/local/stata14/stata
(...other stuff...)
. forval i = 1/100000 {
2. sleep 10
3. di "`i'"
4. }
1
2
3
4
(I hit CTRL+C)
--Break--
r(1);
.


The following python program launches Stata from within python:



def handler(signum, frame):
stata.send_signal(signal.SIGINT)
signal.signal(signal.SIGINT, handler)

stata = subprocess.Popen(["/usr/local/stata14/stata"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setpgrp)

(... other stuff hat handles forwarding stata.stdout to sys.stdout in a separate thread...)

while true:
time.sleep(0.1)
userinput = raw_input(". ")
print(userinput, file=stata.stdin)
stata.stdin.flush()
print(stata.poll())


with the same for loop running in the subprocess, produces:



> /usr/local/stata14/stata
. forval i = 1/100000 {
2. sleep 10
3. di "`i'"
4. }
1
2
3
4
-2
-2
-2


and any further attempt to write to stata.stdin then produces



print(userinput, file=stata.stdin)
IOError: [Errno 32] Broken pipe


Anyone understands why this is happening? I already tried using subprocess.Popen with shell = True. In this case, Popen launches an sh process and a Stata process. If I send SIGINT to the sh process, nothing happens. If I send it to Stata, the same problem as with shell=False arises.



Thanks in advance.










share|improve this question






















  • If you use kill -int instead to simulate what your doing from Python, you'll see the same behavior. Ctrl+C will send sigint to the entire foreground process group.
    – that other guy
    Nov 19 '18 at 12:36










  • I put the subprocess in a different group using preexec_fn=os.setpgrp (hard to spot because the code above does not wrap lines). But it's true that I do observe the same behavior when I use kill -int on the process launched from python. Anyways: how can I replicate the behavior of the process launched without python for the process launched in python?
    – Tobias
    Nov 19 '18 at 12:44












  • Apparently the underlying program just acts differently when it is run in a terminal. So I solved this using pty.fork() instead of subprocess.
    – Tobias
    Nov 20 '18 at 20:50














0












0








0







I am writing a script that is supposed to launch an interactive shell of a third-party program from within python in linux (the third party program is Stata). The purpose of the script is to control the stdin and stdout of the interactive shell so I can access it from an editor and other scripts.



When started from within a linux shell, Stata launches an interactive shell where you can run Stata commands. When I press control+C or use some other way to send a SIGINT signal to Stata, it will stop executing the current command and will return to the interactive shell, but it will not kill the Stata process. I want to replicate this behavior for Stata launched from python.



When I send a SIGINT signal to a Stata process launched from python, it will kill the process instead of just stopping execution of the current command.



The behavior in the shell is:



> /usr/local/stata14/stata
(...other stuff...)
. forval i = 1/100000 {
2. sleep 10
3. di "`i'"
4. }
1
2
3
4
(I hit CTRL+C)
--Break--
r(1);
.


The following python program launches Stata from within python:



def handler(signum, frame):
stata.send_signal(signal.SIGINT)
signal.signal(signal.SIGINT, handler)

stata = subprocess.Popen(["/usr/local/stata14/stata"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setpgrp)

(... other stuff hat handles forwarding stata.stdout to sys.stdout in a separate thread...)

while true:
time.sleep(0.1)
userinput = raw_input(". ")
print(userinput, file=stata.stdin)
stata.stdin.flush()
print(stata.poll())


with the same for loop running in the subprocess, produces:



> /usr/local/stata14/stata
. forval i = 1/100000 {
2. sleep 10
3. di "`i'"
4. }
1
2
3
4
-2
-2
-2


and any further attempt to write to stata.stdin then produces



print(userinput, file=stata.stdin)
IOError: [Errno 32] Broken pipe


Anyone understands why this is happening? I already tried using subprocess.Popen with shell = True. In this case, Popen launches an sh process and a Stata process. If I send SIGINT to the sh process, nothing happens. If I send it to Stata, the same problem as with shell=False arises.



Thanks in advance.










share|improve this question













I am writing a script that is supposed to launch an interactive shell of a third-party program from within python in linux (the third party program is Stata). The purpose of the script is to control the stdin and stdout of the interactive shell so I can access it from an editor and other scripts.



When started from within a linux shell, Stata launches an interactive shell where you can run Stata commands. When I press control+C or use some other way to send a SIGINT signal to Stata, it will stop executing the current command and will return to the interactive shell, but it will not kill the Stata process. I want to replicate this behavior for Stata launched from python.



When I send a SIGINT signal to a Stata process launched from python, it will kill the process instead of just stopping execution of the current command.



The behavior in the shell is:



> /usr/local/stata14/stata
(...other stuff...)
. forval i = 1/100000 {
2. sleep 10
3. di "`i'"
4. }
1
2
3
4
(I hit CTRL+C)
--Break--
r(1);
.


The following python program launches Stata from within python:



def handler(signum, frame):
stata.send_signal(signal.SIGINT)
signal.signal(signal.SIGINT, handler)

stata = subprocess.Popen(["/usr/local/stata14/stata"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setpgrp)

(... other stuff hat handles forwarding stata.stdout to sys.stdout in a separate thread...)

while true:
time.sleep(0.1)
userinput = raw_input(". ")
print(userinput, file=stata.stdin)
stata.stdin.flush()
print(stata.poll())


with the same for loop running in the subprocess, produces:



> /usr/local/stata14/stata
. forval i = 1/100000 {
2. sleep 10
3. di "`i'"
4. }
1
2
3
4
-2
-2
-2


and any further attempt to write to stata.stdin then produces



print(userinput, file=stata.stdin)
IOError: [Errno 32] Broken pipe


Anyone understands why this is happening? I already tried using subprocess.Popen with shell = True. In this case, Popen launches an sh process and a Stata process. If I send SIGINT to the sh process, nothing happens. If I send it to Stata, the same problem as with shell=False arises.



Thanks in advance.







python linux signals sh






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 12:33









Tobias

316315




316315












  • If you use kill -int instead to simulate what your doing from Python, you'll see the same behavior. Ctrl+C will send sigint to the entire foreground process group.
    – that other guy
    Nov 19 '18 at 12:36










  • I put the subprocess in a different group using preexec_fn=os.setpgrp (hard to spot because the code above does not wrap lines). But it's true that I do observe the same behavior when I use kill -int on the process launched from python. Anyways: how can I replicate the behavior of the process launched without python for the process launched in python?
    – Tobias
    Nov 19 '18 at 12:44












  • Apparently the underlying program just acts differently when it is run in a terminal. So I solved this using pty.fork() instead of subprocess.
    – Tobias
    Nov 20 '18 at 20:50


















  • If you use kill -int instead to simulate what your doing from Python, you'll see the same behavior. Ctrl+C will send sigint to the entire foreground process group.
    – that other guy
    Nov 19 '18 at 12:36










  • I put the subprocess in a different group using preexec_fn=os.setpgrp (hard to spot because the code above does not wrap lines). But it's true that I do observe the same behavior when I use kill -int on the process launched from python. Anyways: how can I replicate the behavior of the process launched without python for the process launched in python?
    – Tobias
    Nov 19 '18 at 12:44












  • Apparently the underlying program just acts differently when it is run in a terminal. So I solved this using pty.fork() instead of subprocess.
    – Tobias
    Nov 20 '18 at 20:50
















If you use kill -int instead to simulate what your doing from Python, you'll see the same behavior. Ctrl+C will send sigint to the entire foreground process group.
– that other guy
Nov 19 '18 at 12:36




If you use kill -int instead to simulate what your doing from Python, you'll see the same behavior. Ctrl+C will send sigint to the entire foreground process group.
– that other guy
Nov 19 '18 at 12:36












I put the subprocess in a different group using preexec_fn=os.setpgrp (hard to spot because the code above does not wrap lines). But it's true that I do observe the same behavior when I use kill -int on the process launched from python. Anyways: how can I replicate the behavior of the process launched without python for the process launched in python?
– Tobias
Nov 19 '18 at 12:44






I put the subprocess in a different group using preexec_fn=os.setpgrp (hard to spot because the code above does not wrap lines). But it's true that I do observe the same behavior when I use kill -int on the process launched from python. Anyways: how can I replicate the behavior of the process launched without python for the process launched in python?
– Tobias
Nov 19 '18 at 12:44














Apparently the underlying program just acts differently when it is run in a terminal. So I solved this using pty.fork() instead of subprocess.
– Tobias
Nov 20 '18 at 20:50




Apparently the underlying program just acts differently when it is run in a terminal. So I solved this using pty.fork() instead of subprocess.
– Tobias
Nov 20 '18 at 20:50

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374768%2fsending-sigint-to-process-launched-from-python-does-not-yield-same-result-as-whe%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374768%2fsending-sigint-to-process-launched-from-python-does-not-yield-same-result-as-whe%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules