Abort python processes on Vim exit

You'd think a scripting language could handle portably checking if a pid
is still running but no.
This commit is contained in:
Tim Pope 2014-01-07 23:07:31 -05:00
parent 280b8d09b5
commit 15be24fc82
2 changed files with 17 additions and 7 deletions

View File

@ -164,12 +164,16 @@ function! s:extract_last_stacktrace(nrepl) abort
return stacktrace
endfunction
let s:keepalive = tempname()
call writefile([getpid()], s:keepalive)
function! s:nrepl_dispatch(command, ...) dict abort
let in = 'python'
\ . ' ' . s:shellesc(s:python_dir.'/nrepl_fireplace.py')
\ . ' ' . s:shellesc(a:command)
\ . ' ' . s:shellesc(self.host)
\ . ' ' . s:shellesc(self.port)
\ . ' ' . s:shellesc(s:keepalive)
\ . ' ' . join(map(copy(a:000), 's:shellesc(v:val)'), ' ')
let out = system(in)
if !v:shell_error
@ -228,7 +232,7 @@ def fireplace_check():
def fireplace_repl_dispatch(command, *args):
try:
fireplace_let('out', nrepl_fireplace.dispatch(command, vim.eval('self.host'), vim.eval('self.port'), fireplace_check, *args))
fireplace_let('out', nrepl_fireplace.dispatch(command, vim.eval('self.host'), vim.eval('self.port'), fireplace_check, None, *args))
except Exception, e:
fireplace_let('err', str(e))
EOF

View File

@ -59,14 +59,20 @@ def bdecode(f, char=None):
class Connection:
def __init__(self, host, port, poll=noop):
self.poll = poll
def __init__(self, host, port, custom_poll=noop, keepalive_file=None):
self.custom_poll = custom_poll
self.keepalive_file = keepalive_file
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(8)
s.connect((host, int(port)))
s.setblocking(1)
self.socket = s
def poll(self):
self.custom_poll()
if self.keepalive_file and not os.path.exists(self.keepalive_file):
exit(0)
def close(self):
return self.socket.close()
@ -91,16 +97,16 @@ class Connection:
if 'status' in responses[-1] and 'done' in responses[-1]['status']:
return responses
def dispatch(command, host, port, poll, *args):
conn = Connection(host, port, poll)
def dispatch(command, host, port, poll, keepalive, *args):
conn = Connection(host, port, poll, keepalive)
try:
return getattr(conn, command)(*args)
finally:
conn.close()
def main(command, host, port, *args):
def main(command, host, port, keepalive, *args):
try:
sys.stdout.write(vim_encode(dispatch(command, host, port, noop, *args)))
sys.stdout.write(vim_encode(dispatch(command, host, port, noop, keepalive, *args)))
except Exception, e:
print(e)
exit(1)