Last active
November 29, 2021 17:02
-
-
Save SergeyStretovich/27a16fa2dd778f5da683e05a079ca4d2 to your computer and use it in GitHub Desktop.
A very brief example of parallelism in F#.
This file contains 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
open System | |
open System.IO | |
open System.Drawing | |
let ResizeImage(img:Image):Image= | |
let siz = new Size(60,60) | |
let bmp:Bitmap= new Bitmap(img,siz) | |
let img:Image = bmp :> Image | |
img | |
let DestFileName(pref:string,filePath:string):string = | |
let ffl = new FileInfo(filePath) | |
let nm = pref + ffl.Name | |
let newpath = ffl.DirectoryName+"\\"+nm | |
newpath | |
let FetchAsync (file:string)= | |
async{ | |
try | |
use stream = File.OpenRead(file) | |
let! bytes = stream.AsyncRead(int stream.Length) | |
use memstream = new MemoryStream(bytes) | |
use image = Image.FromStream(memstream) | |
use smallImage = ResizeImage(image) | |
let destFileName = DestFileName("_s2_",file) | |
smallImage.Save(destFileName ) | |
with | |
| (ex:System.Exception) -> | |
printfn "%s" ex.Message | |
} | |
let myFunc = | |
let ddr = new DirectoryInfo (@"G:\IMGS\CATALOG") | |
let getName = Array.map (fun (x:FileInfo) -> x.FullName) | |
let files:string[] = ddr.GetFiles() |> getName | |
let tasks = [for file in files -> FetchAsync(file) ] | |
let parallelTasks = Async.Parallel tasks | |
Async.RunSynchronously parallelTasks |> ignore | |
[<EntryPoint>] | |
let main argv = | |
myFunc | |
System.Console.ReadKey() |> ignore | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment