gpt4 book ai didi

stream - 创建自定义标准输出流

转载 作者:行者123 更新时间:2023-12-05 00:09:56 25 4
gpt4 key购买 nike

我正在使用 LispWorks 的多处理工具(参见 here)。我启动了许多子进程(使用 process-run-function ),其中每个子进程都与一个特定的邮箱相关联。我想要实现的是子进程中标准输出的消息(使用 format )最终在邮箱中,之后我可以在主进程中读取它们。

我会通过替换 *standard-output* 来解决这个问题。带有调用 mailbox-send 的自定义流的子流程的流在格式化的字符串上。但是,我不知道如何创建这样的自定义流。我在这里有哪些选择?

最佳答案

像这样的东西:


(defclass mailbox-input-stream (stream:fundamental-character-input-stream)
((mailbox :initarg :mailbox
:accessor mailbox-stream-mailbox
:initform nil)))

(defmethod stream-element-type ((stream mailbox-input-stream))
'character)

(defmethod stream:stream-read-char ((stream mailbox-input-stream))
(let ((char (mp:mailbox-read (mailbox-stream-mailbox stream))))
(if (eql char #\Line-Separator)
#\Newline
char)))

(defclass mailbox-output-stream (stream:fundamental-character-output-stream)
((mailbox :initarg :mailbox
:accessor mailbox-stream-mailbox
:initform nil)))

(defmethod stream-element-type ((stream mailbox-output-stream))
'character)

(defmethod stream:stream-write-char ((stream mailbox-output-stream) char)
(mp:mailbox-send (mailbox-stream-mailbox stream)
(if (eql char #\Newline)
#\Line-Separator
char)))

(defmethod stream:stream-line-column ((stream mailbox-output-stream))
nil)

(defmethod stream:stream-start-line-p ((stream mailbox-output-stream))
nil)

那么你可以使用 READ-LINE , WRITE-LINE , READ-CHAR , WRITE-CHAR在这样创建的流上:
(make-instance 'mailbox-input-stream :mailbox (mp:process-mailbox process))

(make-instance 'mailbox-output-stream :mailbox other-mailbox)

关于stream - 创建自定义标准输出流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59308920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com