unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* start-process and set-process-filter sequence
@ 2011-04-14  3:10 William Xu
  2011-04-14  5:11 ` Thierry Volpiatto
  0 siblings, 1 reply; 8+ messages in thread
From: William Xu @ 2011-04-14  3:10 UTC (permalink / raw)
  To: help-gnu-emacs

Hi, 

Because set-process-filter will be done after the process is created by
start-process, it seems there is a possibility that the filter may miss
some process output.  Consider this: 

    (defun foo (proc output)
      (setq a output))
    
    (let ((proc (start-process "ls" "ls" "ls")))
      ;; (read-string "Mood: ")
      (setq a nil)
      (set-process-filter proc 'foo))

Compare comment and uncomment the read-string line, `a' would be
different.  

Is there a way to set a process filter before the process starts? so
that we won't miss any output.  


-- 
William

http://xwl.appspot.com




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: start-process and set-process-filter sequence
  2011-04-14  3:10 start-process and set-process-filter sequence William Xu
@ 2011-04-14  5:11 ` Thierry Volpiatto
  2011-04-14  8:22   ` William Xu
  0 siblings, 1 reply; 8+ messages in thread
From: Thierry Volpiatto @ 2011-04-14  5:11 UTC (permalink / raw)
  To: help-gnu-emacs

William Xu <william.xwl@gmail.com> writes:

> Hi, 
>
> Because set-process-filter will be done after the process is created by
> start-process, it seems there is a possibility that the filter may miss
> some process output.  Consider this: 
>
>     (defun foo (proc output)
>       (setq a output))
>     
>     (let ((proc (start-process "ls" "ls" "ls")))
>       ;; (read-string "Mood: ")
>       (setq a nil)
>       (set-process-filter proc 'foo))
>
> Compare comment and uncomment the read-string line, `a' would be
> different.  
>
> Is there a way to set a process filter before the process starts? so
> that we won't miss any output.  
prog1

something like this:

--8<---------------cut here---------------start------------->8---
(prog1
    (start-process "ls" "ls" "ls")
  (set-process-filter (get-process "ls") 'foo)
  (setq a nil)
  ;(read-string "Mood: ")
  )
--8<---------------cut here---------------end--------------->8---

-- 
A+ Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: start-process and set-process-filter sequence
  2011-04-14  5:11 ` Thierry Volpiatto
@ 2011-04-14  8:22   ` William Xu
  2011-04-14 12:02     ` Thien-Thi Nguyen
  2011-04-14 14:02     ` Thierry Volpiatto
  0 siblings, 2 replies; 8+ messages in thread
From: William Xu @ 2011-04-14  8:22 UTC (permalink / raw)
  To: help-gnu-emacs

Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:

   something like this:

   (prog1
       (start-process "ls" "ls" "ls")
     (set-process-filter (get-process "ls") 'foo)
     (setq a nil)
     ;(read-string "Mood: ")
     )

It doesn't seem to really fix the problem.  You moved read-string to the
end, if you try:

   (prog1
       (start-process "ls" "ls" "ls")
     (read-string "Mood: ")
     (set-process-filter (get-process "ls") 'foo)
     (setq a nil))

There will still be a similar problem.  

-- 
William

http://xwl.appspot.com




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: start-process and set-process-filter sequence
  2011-04-14  8:22   ` William Xu
@ 2011-04-14 12:02     ` Thien-Thi Nguyen
  2011-04-15  2:16       ` William Xu
  2011-04-14 14:02     ` Thierry Volpiatto
  1 sibling, 1 reply; 8+ messages in thread
From: Thien-Thi Nguyen @ 2011-04-14 12:02 UTC (permalink / raw)
  To: William Xu; +Cc: help-gnu-emacs

() William Xu <william.xwl@gmail.com>
() Thu, 14 Apr 2011 16:22:35 +0800

   if you try:

      (prog1
          (start-process "ls" "ls" "ls")
        (read-string "Mood: ")
        (set-process-filter (get-process "ls") 'foo)
        (setq a nil))

   There will still be a similar problem.  

Process output is distributed to filters when Emacs has nothing else
to do, such as when pausing for interaction (‘read-string’ et al).
(You can also explicitly request it via ‘accept-process-output’ but
that is not germane.)

So the best strategy is to not allow such pauses in the first place.
E.g., you could add an abstraction ‘start-filtered-process’ and make
sure you use ‘start-filtered-process’ everywhere you'd normally use
a naked ‘start-process’.

(defun start-filtered-process (filter &rest etc)
  "Like `start-process' with ETC, but associate FILTER as well.
Return the newly created process."
  (let ((proc (apply 'start-process etc)))
    ;; Note to programmer: Do NOT tickle Emacs I/O-wise, here.
    ;; [Insert ref to help-gnu-emacs thread, here.]
    (set-process-filter proc filter)
    proc))



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: start-process and set-process-filter sequence
  2011-04-14  8:22   ` William Xu
  2011-04-14 12:02     ` Thien-Thi Nguyen
@ 2011-04-14 14:02     ` Thierry Volpiatto
  2011-04-15  2:21       ` William Xu
  1 sibling, 1 reply; 8+ messages in thread
From: Thierry Volpiatto @ 2011-04-14 14:02 UTC (permalink / raw)
  To: help-gnu-emacs

William Xu <william.xwl@gmail.com> writes:

> Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:
>
>    something like this:
>
>    (prog1
>        (start-process "ls" "ls" "ls")
>      (set-process-filter (get-process "ls") 'foo)
>      (setq a nil)
>      ;(read-string "Mood: ")
>      )
>
> It doesn't seem to really fix the problem.  You moved read-string to the
> end, if you try:
>
>    (prog1
>        (start-process "ls" "ls" "ls")
>      (read-string "Mood: ")
>      (set-process-filter (get-process "ls") 'foo)
>      (setq a nil))
>
> There will still be a similar problem.  
The real problem is: 
why do you want to setq a to nil at this time?

-- 
A+ Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: start-process and set-process-filter sequence
  2011-04-14 12:02     ` Thien-Thi Nguyen
@ 2011-04-15  2:16       ` William Xu
  2011-04-15 10:01         ` Thien-Thi Nguyen
  0 siblings, 1 reply; 8+ messages in thread
From: William Xu @ 2011-04-15  2:16 UTC (permalink / raw)
  To: help-gnu-emacs

Thien-Thi Nguyen <ttn@gnuvola.org> writes:

   Process output is distributed to filters when Emacs has nothing else
   to do, such as when pausing for interaction (‘read-string’ et al).
   (You can also explicitly request it via ‘accept-process-output’ but
   that is not germane.)

   So the best strategy is to not allow such pauses in the first place.
   E.g., you could add an abstraction ‘start-filtered-process’ and make
   sure you use ‘start-filtered-process’ everywhere you'd normally use
   a naked ‘start-process’.

I see, Thanks.  It looks like a bug or shortcoming, though.  I was
expecting the sequence to be similar to this:

,----
| Filter *filter = new Filter;
| Process *proc = new Process(filter);
| proc->start();
`----

-- 
William

http://xwl.appspot.com




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: start-process and set-process-filter sequence
  2011-04-14 14:02     ` Thierry Volpiatto
@ 2011-04-15  2:21       ` William Xu
  0 siblings, 0 replies; 8+ messages in thread
From: William Xu @ 2011-04-15  2:21 UTC (permalink / raw)
  To: help-gnu-emacs

Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:

   The real problem is: 
   why do you want to setq a to nil at this time?

Just for initialize/reset `a' in the testing.  You can move it to the
beginning of `prog1', like: 

  (progn
    (setq a nil)
    (prog1
        (start-process "ls" "ls" "ls")
      (read-string "Mood: ")
      (set-process-filter (get-process "ls") 'foo)))
  
But that is not important here..  

-- 
William

http://xwl.appspot.com




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: start-process and set-process-filter sequence
  2011-04-15  2:16       ` William Xu
@ 2011-04-15 10:01         ` Thien-Thi Nguyen
  0 siblings, 0 replies; 8+ messages in thread
From: Thien-Thi Nguyen @ 2011-04-15 10:01 UTC (permalink / raw)
  To: William Xu; +Cc: help-gnu-emacs

() William Xu <william.xwl@gmail.com>
() Fri, 15 Apr 2011 10:16:09 +0800

   I see, Thanks.  It looks like a bug or shortcoming, though.  I was
   expecting the sequence to be similar to this:

   ,----
   | Filter *filter = new Filter;
   | Process *proc = new Process(filter);
   | proc->start();
   `----

See also ‘make-network-process’, which operates like that.
You can see how its many (many) attributes necessitate a
keyword-args approach.  Probably ‘start-process’ was designed
when things were less complex.



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2011-04-15 10:01 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-04-14  3:10 start-process and set-process-filter sequence William Xu
2011-04-14  5:11 ` Thierry Volpiatto
2011-04-14  8:22   ` William Xu
2011-04-14 12:02     ` Thien-Thi Nguyen
2011-04-15  2:16       ` William Xu
2011-04-15 10:01         ` Thien-Thi Nguyen
2011-04-14 14:02     ` Thierry Volpiatto
2011-04-15  2:21       ` William Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).