From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.help Subject: Re: sending data to an asynchronous process Date: Fri, 02 Apr 2021 22:15:24 +0300 Message-ID: <83zgygebw3.fsf@gnu.org> References: <86v995t2c5.fsf@graner.name> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="6034"; mail-complaints-to="usenet@ciao.gmane.io" To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Fri Apr 02 21:16:13 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lSPHA-0001Uu-Hk for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 02 Apr 2021 21:16:12 +0200 Original-Received: from localhost ([::1]:54778 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lSPH9-0004d7-IL for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 02 Apr 2021 15:16:11 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:39720) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lSPGc-0004bl-1A for help-gnu-emacs@gnu.org; Fri, 02 Apr 2021 15:15:38 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:37828) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lSPGb-00067m-Q9 for help-gnu-emacs@gnu.org; Fri, 02 Apr 2021 15:15:37 -0400 Original-Received: from 84.94.185.95.cable.012.net.il ([84.94.185.95]:3713 helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1lSPGb-0003lk-5G for help-gnu-emacs@gnu.org; Fri, 02 Apr 2021 15:15:37 -0400 In-Reply-To: <86v995t2c5.fsf@graner.name> (message from Nicolas Graner on Fri, 02 Apr 2021 12:20:26 +0200) X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:128821 Archived-At: > From: Nicolas Graner > Date: Fri, 02 Apr 2021 12:20:26 +0200 > > I am writing a program that creates audio samples in an emacs > buffer, then sends them to an external program (sox) to play in > the background while I continue working with emacs. Part of the > code is roughly as follows: > > (setq process > (let ((process-connection-type nil)) > (start-process "my-process" nil > "sox" "-r" rate "-c" channels "-b" bits "-e" encoding "-q" "-d"))) > (process-send-region process start end) > (process-send-eof process) > > The sound plays as expected, but process-send-region does not > return until about half a second before the sound finishes > playing. This means that if I send several minutes of audio, > emacs is stuck during all that time, which completely defeats the > purpose of an asynchronous process. Asynchronous subprocesses mean that their processing and reading their output are asynchronous. Writing to the subprocess is always synchronous. It cannot be any other way, really, unless we start additional threads to write to the subprocess (which then causes problems because the Emacs Lisp machine cannot tolerate more than one thread that has access to Lisp data). What Emacs does is read output from subprocesses while it loops sending the data in chunks, so that at least that part would work -- but that doesn't help in your case. > The solution I found is to use a synchronous process: > (call-process-region start end "sox" nil 0 nil ... > where the fifth argument 0 means to discard process output and > not wait for completion. call-process-region writes the data to a temporary file, and then invokes the subprocess. So if you do the same "by hand" and then call start-process, you will have the best of all worlds. IOW, the temporary file solution you didn't like is exactly what call-process-region does under the hood, and so efficiency is not the issue here. As for cleaning up the temporary files, that isn't really a problem, is it?