Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting subprocesses in racket

I would like to start n subprocesses from within Dr Racket or from the command line (i.e. I would like to run the code either entirely within the Dr Racket ide or entirely from the command line).

These processes would then exchange messages through stdin and stdout.

Is this possible by modifying the following code, which starts them from the cmd line? (or is it possible at all? Note that I am a scheme beginner)

(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)))
like image 423
user35202 Avatar asked Oct 15 '25 19:10

user35202


1 Answers

Not sure if this is what you're after, but current-command-line-arguments is a parameter so you can set it with parameterize.

Here's an adaptation of your code that produces the files in the current directory:

#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))))))

Btw, DrRacket is just the name of the IDE. Racket is the name of the language.

like image 103
stchang Avatar answered Oct 18 '25 22:10

stchang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!