* waiting for asynchronous processes
@ 2011-10-15 17:20 Perry Smith
2011-10-15 22:24 ` Štěpán Němec
0 siblings, 1 reply; 8+ messages in thread
From: Perry Smith @ 2011-10-15 17:20 UTC (permalink / raw)
To: help-gnu-emacs List
[-- Attachment #1: Type: text/plain, Size: 671 bytes --]
I was trying to write a keyboard macro that included using grep and the first hit that it found. The problem was that I had to wait for the grep to finish. I'm not sure why but (sit-for 1) didn't work.
I started looking for a built in way to wait for an asynchronous command to complete and discovered that I had written one long long time ago in a galaxy far far away.
> (defun wait-for-async-process ( proc )
> "Wait for PROC to finish"
> (while (null (eq (process-status proc) 'exit))
> (accept-process-output)))
I went ahead and looked for something like it in the distribution but didn't see it.
I'm curious if I overlooked it.
pedz
[-- Attachment #2: Type: text/html, Size: 1250 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: waiting for asynchronous processes
2011-10-15 17:20 waiting for asynchronous processes Perry Smith
@ 2011-10-15 22:24 ` Štěpán Němec
2011-10-16 0:17 ` Perry Smith
0 siblings, 1 reply; 8+ messages in thread
From: Štěpán Němec @ 2011-10-15 22:24 UTC (permalink / raw)
To: Perry Smith; +Cc: help-gnu-emacs List
On Sat, 15 Oct 2011 19:20:44 +0200
Perry Smith wrote:
> I started looking for a built in way to wait for an asynchronous command to
> complete and discovered that I had written one long long time ago in a galaxy
> far far away.
>
>> (defun wait-for-async-process ( proc )
>> "Wait for PROC to finish"
>> (while (null (eq (process-status proc) 'exit))
>> (accept-process-output)))
Looks like a terrible hack to me.
> I went ahead and looked for something like it in the distribution but didn't see it.
>
> I'm curious if I overlooked it.
I doubt it. If you want to wait for the process in the way you seem to
want to wait for it, why don't you just use a synchronous process to
begin with? If you _do_ want the non-waiting advantage of asynchronous
processes, the usual way to handle state changes (s.a. when the process
finishes) is to use sentinels, see e.g. (info "(elisp)Sentinels").
--
Štěpán
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: waiting for asynchronous processes
2011-10-15 22:24 ` Štěpán Němec
@ 2011-10-16 0:17 ` Perry Smith
2011-10-16 1:15 ` Štěpán Němec
0 siblings, 1 reply; 8+ messages in thread
From: Perry Smith @ 2011-10-16 0:17 UTC (permalink / raw)
To: Štěpán Němec; +Cc: help-gnu-emacs List
On Oct 15, 2011, at 5:24 PM, Štěpán Němec wrote:
> On Sat, 15 Oct 2011 19:20:44 +0200
> Perry Smith wrote:
>
>> I started looking for a built in way to wait for an asynchronous command to
>> complete and discovered that I had written one long long time ago in a galaxy
>> far far away.
>>
>>> (defun wait-for-async-process ( proc )
>>> "Wait for PROC to finish"
>>> (while (null (eq (process-status proc) 'exit))
>>> (accept-process-output)))
>
> Looks like a terrible hack to me.
>
>> I went ahead and looked for something like it in the distribution but didn't see it.
>>
>> I'm curious if I overlooked it.
>
> I doubt it. If you want to wait for the process in the way you seem to
> want to wait for it, why don't you just use a synchronous process to
> begin with? If you _do_ want the non-waiting advantage of asynchronous
> processes, the usual way to handle state changes (s.a. when the process
> finishes) is to use sentinels, see e.g. (info "(elisp)Sentinels").
Well, as I said, what I really wanted to use is "grep" (the built in emacs function).
I assumed I couldn't reach in and change him from asynchronous to synchronous -- can I?
pedz
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: waiting for asynchronous processes
2011-10-16 0:17 ` Perry Smith
@ 2011-10-16 1:15 ` Štěpán Němec
2011-10-16 1:49 ` Perry Smith
0 siblings, 1 reply; 8+ messages in thread
From: Štěpán Němec @ 2011-10-16 1:15 UTC (permalink / raw)
To: Perry Smith; +Cc: help-gnu-emacs List
On Sun, 16 Oct 2011 02:17:03 +0200
Perry Smith wrote:
> Well, as I said, what I really wanted to use is "grep" (the built in emacs function).
> I assumed I couldn't reach in and change him from asynchronous to synchronous -- can I?
I don't think you can, indeed. `grep' uses `compilation-start', which
only starts a synchronous process when asynchronous processes are not
available.
Depending on what exactly it is you're after, it might be better to just
define your own command, or try to modify the sentinel of the grep
process (the `grep' command returns the compilation buffer; you can get
at the associated process and its sentinel using the usual functions),
which, when done cleanly, would actually require defining another
command, too.
Looking at `compilation-handle-exit', even the default sentinel provides
several hooks for customising what happens when the process finishes,
namely `compilation-exit-message-function' and
`compilation-finish-function(s)', so just using (one of) these variables
might be sufficient for your needs.
--
Štěpán
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: waiting for asynchronous processes
2011-10-16 1:15 ` Štěpán Němec
@ 2011-10-16 1:49 ` Perry Smith
2011-10-16 9:16 ` Štěpán Němec
2011-10-16 9:21 ` Thien-Thi Nguyen
0 siblings, 2 replies; 8+ messages in thread
From: Perry Smith @ 2011-10-16 1:49 UTC (permalink / raw)
To: Štěpán Němec; +Cc: help-gnu-emacs List
On Oct 15, 2011, at 8:15 PM, Štěpán Němec wrote:
> On Sun, 16 Oct 2011 02:17:03 +0200
> Perry Smith wrote:
>
>> Well, as I said, what I really wanted to use is "grep" (the built in emacs function).
>> I assumed I couldn't reach in and change him from asynchronous to synchronous -- can I?
>
> I don't think you can, indeed. `grep' uses `compilation-start', which
> only starts a synchronous process when asynchronous processes are not
> available.
>
> Depending on what exactly it is you're after, it might be better to just
> define your own command, or try to modify the sentinel of the grep
> process (the `grep' command returns the compilation buffer; you can get
> at the associated process and its sentinel using the usual functions),
> which, when done cleanly, would actually require defining another
> command, too.
>
> Looking at `compilation-handle-exit', even the default sentinel provides
> several hooks for customising what happens when the process finishes,
> namely `compilation-exit-message-function' and
> `compilation-finish-function(s)', so just using (one of) these variables
> might be sufficient for your needs.
Thanks.
One more question, not sure how to ask it.
I was hooking this into a keyboard macro so I basically wanted to "do nothing" while I waited for the process to complete. What is the proper way to "do nothing" in emacs?
Thanks
pedz
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: waiting for asynchronous processes
2011-10-16 1:49 ` Perry Smith
@ 2011-10-16 9:16 ` Štěpán Němec
2011-10-16 12:49 ` Perry Smith
2011-10-16 9:21 ` Thien-Thi Nguyen
1 sibling, 1 reply; 8+ messages in thread
From: Štěpán Němec @ 2011-10-16 9:16 UTC (permalink / raw)
To: Perry Smith; +Cc: help-gnu-emacs List
On Sun, 16 Oct 2011 03:49:51 +0200
Perry Smith wrote:
> I was hooking this into a keyboard macro so I basically wanted to "do nothing"
> while I waited for the process to complete. What is the proper way to "do
> nothing" in emacs?
Well, `sleep-for' is quite close to "do nothing" I suppose, but I don't
think that's what you want -- you want the process to run and other
stuff to happen when the process finishes, so just staring at the screen
until then seems a more appropriate "do nothing" implementation here.
I also don't think a keyboard macro is suitable for what you're doing.
You will at least need to bind some variable(s) around the `grep'
invocation, and the only way I can think of to do that with a keyboard
macro only would be something like M-:, which would be crazy. Why don't
you just define a command and bind it to a key?
--
Štěpán
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: waiting for asynchronous processes
2011-10-16 1:49 ` Perry Smith
2011-10-16 9:16 ` Štěpán Němec
@ 2011-10-16 9:21 ` Thien-Thi Nguyen
1 sibling, 0 replies; 8+ messages in thread
From: Thien-Thi Nguyen @ 2011-10-16 9:21 UTC (permalink / raw)
To: help-gnu-emacs List
() Perry Smith <pedzsan@gmail.com>
() Sat, 15 Oct 2011 20:49:51 -0500
What is the proper way to "do nothing" in emacs?
program writer here.
i do nothing in emacs.
(improperly, though.)
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: waiting for asynchronous processes
2011-10-16 9:16 ` Štěpán Němec
@ 2011-10-16 12:49 ` Perry Smith
0 siblings, 0 replies; 8+ messages in thread
From: Perry Smith @ 2011-10-16 12:49 UTC (permalink / raw)
To: Štěpán Němec; +Cc: help-gnu-emacs List
On Oct 16, 2011, at 4:16 AM, Štěpán Němec wrote:
> On Sun, 16 Oct 2011 03:49:51 +0200
> Perry Smith wrote:
>
>> I was hooking this into a keyboard macro so I basically wanted to "do nothing"
>> while I waited for the process to complete. What is the proper way to "do
>> nothing" in emacs?
>
> Well, `sleep-for' is quite close to "do nothing" I suppose, but I don't
> think that's what you want -- you want the process to run and other
> stuff to happen when the process finishes, so just staring at the screen
> until then seems a more appropriate "do nothing" implementation here.
>
> I also don't think a keyboard macro is suitable for what you're doing.
> You will at least need to bind some variable(s) around the `grep'
> invocation, and the only way I can think of to do that with a keyboard
> macro only would be something like M-:, which would be crazy. Why don't
> you just define a command and bind it to a key?
Well... I was trying to gently steer you to the errors of your ways
but you seem to be missing the light.
First, you missed the point that I was using grep. Instead you said I
should be using a synchronous process. You missed that I wasn't
writing the code that started the process but using a pre-existing
and commonly used routine.
Thus, I could not reach in and change from async to sync. I had to
ask that on a follow up question (which was trying to gently get you
to discover that you had not suggested anything useful). It appeared
to me as if you had not read my original post. I hoped, perhaps, you
would go back and re-read it. You definitely seem to have missed
the complexity of what I had accomplished.
The second problem is, ok.... suppose I write a sentinels and it will tell
me when the process is done... Now what do I do? I write a wrapper
that does nothing until that sentinel tells me its ok to proceed. You have
only added another layer and accomplished nothing.
According to (info "(elisp)Sentinels"), the best way to do
nothing is accept-process-output -- which is what I had and is also
really the only key question. That should have clued you in.
But no, you still keep coming. So...
So... third: it is fairly common (for me) to do M-: (translated from <escape> :)
inside a macro and put in some lisp code to set variables or do whatever
you need. It may not be neat and tidy but it gets the job done.
I did write a small glue function in *scratch* but that is mostly beside
the point. In the keyboard function, I used straight lisp. Happens all
the time.
So, lets review: my original post wasn't asking for help on how to do it -- I had
already accomplished what I wanted to do. I was asking if there was a
better way. You offered a rather stern judgement but still have not
offered a better way.
None of these things appears to have occurred to you.
Hope this helps
pedz
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2011-10-16 12:49 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-10-15 17:20 waiting for asynchronous processes Perry Smith
2011-10-15 22:24 ` Štěpán Němec
2011-10-16 0:17 ` Perry Smith
2011-10-16 1:15 ` Štěpán Němec
2011-10-16 1:49 ` Perry Smith
2011-10-16 9:16 ` Štěpán Němec
2011-10-16 12:49 ` Perry Smith
2011-10-16 9:21 ` Thien-Thi Nguyen
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.