我想从Dr Racket或命令行中启动n个子进程(即我想完全在Dr Racket ide中运行代码或完全从命令行运行代码。)
然后,这些进程将通过stdin和stdout交换消息。
这可以通过修改以下代码来实现吗,这些代码是从cmd行开始的吗? (或者它是否可能?注意我是计划初学者)
(struct proc (stdout stdin))
(define (start-program p)
(define-values (s stdout stdin stderr) (subprocess #f #f #f p))
(thread (lambda () (copy-port stderr (current-error-port))))
(proc stdout stdin))
(define programs (vector->list (current-command-line-arguments)))
(map start-program programs)
(define (send-to proc v)
(write v (proc-stdin proc))
(flush-output (proc-stdin proc)))
(define (receive-from proc)
(read (proc-stdout proc)))
答案 0 :(得分:1)
不确定这是否是您所追求的,但current-command-line-arguments
是一个参数,因此您可以使用parameterize
进行设置。
以下是对您的代码进行修改,以生成当前目录中的文件:
#lang racket
(struct proc (stdout stdin))
(define (start-program p)
(define-values (s stdout stdin stderr) (subprocess #f #f #f p))
(thread (lambda () (copy-port stderr (current-error-port))))
(proc stdout stdin))
(define (send-to proc v)
(write v (proc-stdin proc))
(flush-output (proc-stdin proc)))
(define (receive-from proc)
(read (proc-stdout proc)))
(parameterize ([current-command-line-arguments (vector "ls")])
(define programs
(map find-executable-path (vector->list (current-command-line-arguments))))
(define running-programs
(map start-program programs))
(let loop ([x (receive-from (first running-programs))])
(displayln x)
(unless (eof-object? x)
(loop (receive-from (first running-programs))))))
顺便说一句,DrRacket只是IDE的名称。 Racket是该语言的名称。