I have a job_start()
call like this:
let options = {
\ 'stdoutbuffer': [],
\ 'handler': a:handler,
\ }
let command = s:build_command(a:cmd)
let s:job = job_start(command, {
\ 'out_cb': function('s:on_stdout_vim', options),
\ 'close_cb': function('s:on_close_vim', options),
\ 'exit_cb': function('s:on_exit_vim', options)
\ })
The external command being run is ps -axco command
.
Normally when the job runs, the out_cb
is invoked multiple times, then the close_cb
once, and finally the exit_cb
once.
However when my code is invoked twice in rapid succession the first job's close_cb
is never invoked. I haven't been able to figure out why.
This is a problem because the job result is handled in my close_cb
handler (I only added an exit_cb
to try to debug this problem). So if the close_cb
handler isn't invoked the result of the job is lost.
The docs says that exit_cb
is called when the job ends and close_cb
is called when the channel is closed. So exit_cb
sounds more appropriate because I care about the job, not the underlying channel.
However the exit_cb
docs also say that:
data can be buffered, callbacks may still be called after the process ends.
I think that's saying that out_cb
can be called after exit_cb
. If so, it would suggest that I can't rely on exit_cb
to know when I have everything produced by the job on stdout.
Furthermore the close_cb
docs say that:
Vim will invoke callbacks that handle data before invoking close_cb
Which sounds like I can rely on having all the data when close_cb
is invoked. And this is why I'm using close_cb
instead of exit_cb
to act on the job's result. Except close_cb
doesn't get invoked when this other plugin's job is around.
Am I right to use close_cb
not exit_cb
? If so, why would it not be invoked? When would it be right to use exit_cb
?
exit_cb
handler, and writesleep 1m
at the top (to give some time to pending callbacks)?exit_cb
would work here, though the 1ms sleep is a bit arbitrary, but I'd like to understand whyclose_cb
doesn't get called.sleep 1m
is not that arbitrary; it's the least amount of time Vim can sleep, and in my experience, it's enough for all pending callbacks to have been processed.sleep 1m
(or even10m
doesn't help). I'll keep trying to produce a minimal example.