Go to the first, previous, next, last section, table of contents.


Creating an Asynchronous Process

After an asynchronous process is created, Emacs and the subprocess both continue running immediately. The process thereafter runs in parallel with Emacs, and the two can communicate with each other using the functions described in following sections. However, communication is only partially asynchronous: Emacs sends data to the process only when certain functions are called, and Emacs accepts data from the process only when Emacs is waiting for input or for a time delay.

Here we describe how to create an asynchronous process.

Function: start-process name buffer-or-name program &rest args
This function creates a new asynchronous subprocess and starts the program program running in it. It returns a process object that stands for the new subprocess in Lisp. The argument name specifies the name for the process object; if a process with this name already exists, then name is modified (by appending `<1>', etc.) to be unique. The buffer buffer-or-name is the buffer to associate with the process.

The remaining arguments, args, are strings that specify command line arguments for the program.

In the example below, the first process is started and runs (rather, sleeps) for 100 seconds. Meanwhile, the second process is started, and given the name `my-process<1>' for the sake of uniqueness. It inserts the directory listing at the end of the buffer `foo', before the first process finishes. Then it finishes, and a message to that effect is inserted in the buffer. Much later, the first process finishes, and another message is inserted in the buffer for it.

(start-process "my-process" "foo" "sleep" "100")
     => #<process my-process>

(start-process "my-process" "foo" "ls" "-l" "/user/lewis/bin")
     => #<process my-process<1>>

---------- Buffer: foo ----------
total 2
lrwxrwxrwx  1 lewis     14 Jul 22 10:12 gnuemacs --> /emacs
-rwxrwxrwx  1 lewis     19 Jul 30 21:02 lemon

Process my-process<1> finished

Process my-process finished
---------- Buffer: foo ----------

Function: start-process-shell-command name buffer-or-name command &rest command-args
This function is like start-process except that it uses a shell to execute the specified command. The argument command is a shell command name, and command-args are the arguments for the shell command. The variable shell-file-name specifies which shell to use.

The point of running a program through the shell, rather than directly with start-process, is so that you can employ shell features such as wildcards in the arguments. It follows that if you include an arbitrary user-specified filename in the command, you should quote it with shell-quote-argument first, so that any special shell characters in the file name do not have their special shell meanings. See section Shell Arguments.

Variable: process-connection-type
This variable controls the type of device used to communicate with asynchronous subprocesses. If it is non-nil, then PTYs are used, when available. Otherwise, pipes are used.

PTYs are usually preferable for processes visible to the user, as in Shell mode, because they allow job control (C-c, C-z, etc.) to work between the process and its children, whereas pipes do not. For subprocesses used for internal purposes by programs, it is often better to use a pipe, because they are more efficient. In addition, the total number of PTYs is limited on many systems and it is good not to waste them.

The value process-connection-type is used when start-process is called. So you can specify how to communicate with one subprocess by binding the variable around the call to start-process.

(let ((process-connection-type nil))  ; Use a pipe.
  (start-process ...))

To determine whether a given subprocess actually got a pipe or a PTY, use the function process-tty-name (see section Process Information).


Go to the first, previous, next, last section, table of contents.