JNe*_*ens 4 common-lisp stream multiprocessing lispworks
我正在使用 LispWorks 的多处理工具(请参阅此处)。我启动了许多子进程(使用process-run-function),其中每个子进程都与一个特定的邮箱相关联。我想要实现的是format子进程中标准输出(使用)的消息最终在邮箱中,之后我可以在主进程中读取它们。
我将通过用*standard-output*调用mailbox-send格式化字符串的自定义流替换子流程的流来解决这个问题。但是,我不知道如何创建这样的自定义流。我在这里有哪些选择?
小智 6
像这样的东西:
(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)
Run Code Online (Sandbox Code Playgroud)
然后你可以在这样创建的流上使用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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
86 次 |
| 最近记录: |