From c606c71cb77a044b2bc34210f5eac719ba75354f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?c=C3=A6l=C5=8Drum=20spect=C4=81tr=C4=ABx?= Date: Fri, 21 Nov 2025 21:44:43 +0100 Subject: [PATCH] Add parallel downloads --- rain.scm | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/rain.scm b/rain.scm index 28fb8cf..a373d91 100755 --- a/rain.scm +++ b/rain.scm @@ -3,13 +3,16 @@ exec csi -ss "$0" "$@" |# +(define parallel 1) + (import (chicken io) (chicken format) (chicken port) (chicken process-context) (html-parser) (http-client) - (matchable)) + (matchable) + (srfi-18)) (define (find-comic-image index) (call-with-current-continuation @@ -27,16 +30,28 @@ exec csi -ss "$0" "$@" (call-with-input-request (find-comic-image index) #f (lambda (in) - (copy-port in out)))))) + (copy-port in out))))) + (format (current-error-port) "* downloaded comic image ~a~%" index)) (define (download-all indices) - (for-each download-comic-image indices)) + (format (current-error-port) "* using ~a threads~%" parallel) + (for-each download-batch (chop indices parallel))) + +(define (download-batch batch) + (let ((threads (map (lambda (index) + (make-thread + (lambda () + (download-comic-image index)))) + batch))) + (for-each thread-start! threads) + (for-each thread-join! threads))) (define (usage) - (format (current-error-port) "usage: ~a [OPTION ...] [INDEX ...]~%" (program-name)) + (format (current-error-port) "usage: ~a [OPTION ...] INDEX ...~%" (program-name)) (format (current-error-port) "options:~%") - (format (current-error-port) " -h|-help display this help message~%") - (format (current-error-port) " -v|-version display version information~%")) + (format (current-error-port) " -h|-help display this help message~%") + (format (current-error-port) " -v|-version display version information~%") + (format (current-error-port) " -j|-parallel N download N images in parallel~%")) (define parse-command-line (match-lambda @@ -44,6 +59,13 @@ exec csi -ss "$0" "$@" (usage)) (((or "-v" "-version") . _) (format (current-error-port) "rain.scm 1.0.0~%" (program-name))) + (((or "-j" "-parallel") n . rest) + (let ((nn (string->number n))) + (unless nn + (usage) + (exit 1)) + (set! parallel nn) + (parse-command-line rest))) (() (usage)) (indices