##// END OF EJS Templates
Merge pull request #1945 from tkf/script-proc...
Min RK -
r7567:920ee654 merge
parent child Browse files
Show More
@@ -15,6 +15,8 b''
15 import os
15 import os
16 import re
16 import re
17 import sys
17 import sys
18 import signal
19 import time
18 from subprocess import Popen, PIPE
20 from subprocess import Popen, PIPE
19
21
20 # Our own packages
22 # Our own packages
@@ -58,6 +60,12 b' def script_args(f):'
58 with --out/err.
60 with --out/err.
59 """
61 """
60 ),
62 ),
63 magic_arguments.argument(
64 '--proc', type=str,
65 help="""The variable in which to store Popen instance.
66 This is used only when --bg option is given.
67 """
68 ),
61 ]
69 ]
62 for arg in args:
70 for arg in args:
63 f = arg(f)
71 f = arg(f)
@@ -193,9 +201,32 b' class ScriptMagics(Magics, Configurable):'
193 if args.err:
201 if args.err:
194 self.shell.user_ns[args.err] = p.stderr
202 self.shell.user_ns[args.err] = p.stderr
195 self.job_manager.new(self._run_script, p, cell)
203 self.job_manager.new(self._run_script, p, cell)
204 if args.proc:
205 self.shell.user_ns[args.proc] = p
196 return
206 return
197
207
198 out, err = p.communicate(cell)
208 try:
209 out, err = p.communicate(cell)
210 except KeyboardInterrupt:
211 try:
212 p.send_signal(signal.SIGINT)
213 time.sleep(0.1)
214 if p.poll() is not None:
215 print "Process is interrupted."
216 return
217 p.terminate()
218 time.sleep(0.1)
219 if p.poll() is not None:
220 print "Process is terminated."
221 return
222 p.kill()
223 print "Process is killed."
224 except OSError:
225 pass
226 except Exception as e:
227 print "Error while terminating subprocess (pid=%i): %s" \
228 % (p.pid, e)
229 return
199 out = py3compat.bytes_to_str(out)
230 out = py3compat.bytes_to_str(out)
200 err = py3compat.bytes_to_str(err)
231 err = py3compat.bytes_to_str(err)
201 if args.out:
232 if args.out:
General Comments 0
You need to be logged in to leave comments. Login now