jed-users mailing list

[2024 Date Index] [2024 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [jed-users] A couple of questions about the new_process() function


Still alive.

Morten Bo Johansen <mbj@xxxxxxxxx> wrote:
> Hopefully everybody is still alive ;)
>
> If I use the new_process() function to run some command, I may
> catch its exit status by appending a ".wait() to the command,
> e.g.
>
>   variable obj = new_process([cmd], write=1).wait()

The new_process function returns a structure that contains details
about the newly created process, including any file descriptors
generated by the call. Additionally, it features a "wait" field that
references the waitpid function. In the example given, you invoked the
waitpid function and received the object it returned. However, this
object is distinct from the one returned by the new_process function.

> returns the following fields
>
>   pid
>   exited
>   exit_status
>   signal
>   coredump
>   stopped
>   continued

Correct.  This is the object returned by the waitpid function.

> but not the object field, fp1

Right.  That field is part of the new_process object, and not the
waitpid one.

>
> If I do
>
>   variable obj = new_process([cmd], write=1);
>   obj.wait();
>
> I get the fp1 object field but not e.g. the exit_status field.

Yes, you have assigned obj to the structure returned by new_process,
but you have left the one returned by waitpid on the stack.

> What wait method shall I specifiy in order to get both?

You need to make separate calls:

     p = new_process (....);
     w = p.wait ();

> Second question: Can new_process() act as a replacement for the
> open_process() function?

It depends.  The new_process function knows nothing about jed's
buffers.  So if you want the output from a command invoked by the
new_process function to get written to a buffer, you would have to
write that code yourself.

>
> With the open_process() function, I can start a persistent
> process and attach it to a buffer and then communicate with it
> and capture the output without having to start and stop the
> process every time. I would like to perhaps simplify and maybe
> also "robustify" the flyspelling function in my aspell.sl
> extension, but I can't seem to figure out how to do it. The
> documentation for new_process() does not cite any examples for
> using it as a replacement for open_process(). The command I use
> with the open_process() function is (as an example)
>
>   variable pid = open_process ("aspell", "-d", "en", "-a", 3);
>
> I may tie this process to a buffer and then send a word to it
> and capture aspell's response as to whether the word was
> misspelled or not.
>
> Can I do something similar with new_process()?

I believe so.  You would want to using something like:

   argv = ["aspell", "-d", "en", "-a"];
   variable pobj = new_process (argv; write=1, read=0);

   % Interact with it by writing to pobj.fp0 and reading from pobj.fp1

   % When finished, close the pipe and collect its return status:
   () = fclose (pobj.fp0);
   w = pobj.wait();

Note that you have to be careful using this approach to avoid deadlock
where, e.g., the aspell process is waiting for input while you are
also trying to read from it.  So you will want to make sure that you
have everything synchronized.

Also keep in mind that Ctrl-g will generate a SIGINT signal that will
be sent to jed and all of its subprocesses.  The open_process function
places all of the processes created by it into their own process
groups to avoid this issue.  You way want to do the same for the
aspell process.  It would go something like this:

  private define aspell_preexec_hook (fdlist)
  {
     % Put the process into its own process group
     () = setpgid (getpid(), 0);
  }
  p = new_process (argv; write=1, read=0, pre_exec_hook=&aspell_preexec_hook);

Dealing with subprocesses can be very tricky.  The open_process
function takes care of a lot of the details for you.

I hope this helps.
Thanks,
--John

> Regards,
> Morten
> _______________________________________________
> For list information, visit <http://jedsoft.org/jed/mailinglists.html>.
>
_______________________________________________
For list information, visit <http://jedsoft.org/jed/mailinglists.html>.


[2024 date index] [2024 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]