Created
July 31, 2012 17:10
-
-
Save wavewave/3218615 to your computer and use it in GitHub Desktop.
slow copy: using zipped conduit sinks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Control.Concurrent | |
| import Control.Monad | |
| import Control.Monad.Trans | |
| import qualified Data.ByteString as B | |
| import Data.Conduit | |
| import Data.Conduit.Binary as CB | |
| import qualified Data.Conduit.List as CL | |
| import qualified Data.Conduit.Util as CU | |
| import System.Environment | |
| import System.Exit | |
| import System.IO | |
| waitSink :: Int -> Int -> Sink B.ByteString IO Int | |
| waitSink nbytes niter = do | |
| CB.drop nbytes | |
| mp <- CL.peek | |
| liftIO $ putStrLn $ "iteration number = " ++ show niter | |
| liftIO $ threadDelay 50000 | |
| case mp of | |
| Nothing -> return niter | |
| Just _ -> waitSink nbytes (niter+1) | |
| main = do | |
| args <- getArgs | |
| when (length args /= 2) $ do | |
| putStrLn "num of args must be 2" | |
| exitWith (ExitFailure 1) | |
| let srcfile = args !! 0 | |
| tgtfile = args !! 1 | |
| putStrLn $ "slow copying " ++ srcfile ++ " to " ++ tgtfile | |
| ih <- openFile srcfile ReadMode | |
| oh <- openFile tgtfile WriteMode | |
| sourceHandle ih $$ CU.zipSinks (waitSink 1024 0) (sinkHandle oh) | |
| -- r <- sourceHandle ih $$ waitSink 1024 0 | |
| -- print r | |
| hClose oh | |
| hClose ih |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment