From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Nicolas Graner Newsgroups: gmane.emacs.help Subject: sending data to an asynchronous process Date: Fri, 02 Apr 2021 12:20:26 +0200 Message-ID: <86v995t2c5.fsf@graner.name> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="18990"; 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 12:20:56 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 1lSGv9-0004qD-B4 for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 02 Apr 2021 12:20:55 +0200 Original-Received: from localhost ([::1]:51594 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lSGv8-0004l9-89 for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 02 Apr 2021 06:20:54 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:36216) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lSGun-0004ko-Q6 for help-gnu-emacs@gnu.org; Fri, 02 Apr 2021 06:20:33 -0400 Original-Received: from smtp26.services.sfr.fr ([93.17.128.190]:35276) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lSGuk-0005xz-HV for help-gnu-emacs@gnu.org; Fri, 02 Apr 2021 06:20:33 -0400 Original-Received: from hypra-graner (1.124.67.86.rev.sfr.net [86.67.124.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by msfrf2603.sfr.fr (SMTP Server) with ESMTPS id E00AB1C003C32 for ; Fri, 2 Apr 2021 12:20:27 +0200 (CEST) X-mail-filterd: 1.0.0 X-sfr-mailing: LEGIT X-sfr-spamrating: 40 X-sfr-spam: not-spam DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=graner.name; s=202006; t=1617358827; h=From:To:Subject:Date; bh=j+W0/zetbLZsysdDptTqu5rC24/M6Xuv+pT rDxmJGjc=; b=RgWpZVPmQb2Y5jVdthwTQtxAg5kOmpEjwJIRPi2zHSyZLWDsxmJEsCP8UTarGez VfTa+DDy+jwC8UKjrB6fLgr4ylz4/c1JOUk+WvRdtBW7oroaFFyp91B+Wj95S9VuHXfOufE+ds2H R152PJJepi4L4vAuZA/HqUFJZsO2ZRGF0lGD+JDgE3VvYGe631248kapACdkMEKlfk1+z8DANyjm L3+2eoFgMPTZpWelIbhUbn/T4GsdSGLy8gcPKXZXTfhQLW9poOeVWGqY9XzGZplL1ir6ucbsMJAX Jj8dSsZ4JGPxZPrNMCjw0OrmuA8cMwLASXKKMxekWOtK0cFV1IQ==; Original-Received: from hypra-graner (1.124.67.86.rev.sfr.net [86.67.124.1]) by msfrf2603.sfr.fr (SMTP Server) with ESMTP id BAD551C000407 for ; Fri, 2 Apr 2021 12:20:27 +0200 (CEST) Original-Received: from hypra-graner (1.124.67.86.rev.sfr.net [86.67.124.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by msfrf2603.sfr.fr (SMTP Server) with ESMTPS for ; Fri, 2 Apr 2021 12:20:27 +0200 (CEST) Received-SPF: pass client-ip=93.17.128.190; envelope-from=nicolas.graner@sfr.fr; helo=smtp26.services.sfr.fr X-Spam_score_int: -13 X-Spam_score: -1.4 X-Spam_bar: - X-Spam_report: (-1.4 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action 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:128804 Archived-At: 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. Using process-send-string instead of process-send-region makes no difference. I suppose it has to do with buffering on the pipe, bit I don't know if I have any control over this. I tried to use stdbuf, as in: (start-process "my-process" nil "stdbuf" "-i10M" "-o10M" "sox" ... which makes no difference. One possible approach would be to send the data in small chunks, arranging for some sort of sentinel to be called when a chunk has been consumed, to send the nex one. Is there any way to do this? I could also write the data into a temporary file and let sox read from it but that seems pretty inefficient and would require careful cleanup to not clutter the disk. 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. This works and does essentially what I want, except that I don't know the PID of the spawned process so I cannot communicate with it, e.g. to stop it before the end or determine whether it's still runnin or not. I'd appreciate any insights and/or solutions? FYI, running emacs 28.0.50 on debian 5.5. Thanks, Nicolas