Next: , Previous: , Up: Processes   [Contents][Index]


57.3 Creating an Asynchronous Process

After an asynchronous process is created, SXEmacs and the Lisp program both continue running immediately. The process may thereafter run in parallel with SXEmacs, and the two may communicate with each other using the functions described in following sections. Here we describe how to create an asynchronous process with start-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 adding ‘<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.

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. A rule of thumb is to use ptys for processes the user interacts with directly, and pipes for processes that are hidden from the user.

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 Process Information).

Lisp functions that manipulate processes usually accept a process argument. Besides using an actual process object for this argument, you can use a process name, a buffer object, the name of a buffer, or nil. Specifying a buffer or buffer name for the process argument means use the process associated with the buffer (or the most recent one, if there is more than one). nil means use the process associated with the current buffer. See Process Information. See Process Buffers.


Next: , Previous: , Up: Processes   [Contents][Index]