F # Lesson 5: asynchronous Workflow

Source: Internet
Author: User

Before you can write out your learning experience, write about expert F # About AsynchronizationProgramOfArticleExamples of outdated syntax and corresponding improvements

 

Code

# Light
Open System
Open System. Io
Open Microsoft. fsharp. Control
Open Microsoft. fsharp. Control. commonextensions

LetNumimages =200
LetSize =512
LetNumpixels = size * size

LetTransfromimage (pixels, imagenum) =
Printfn"Transformimage % d"Imagenum
//Frequent CPU computing operations
Pixels|> (Array. Map (FunB->B + 1uy ))

Let Processimageasync (I) =
Async {
Use instream = file. openread (sprintf " Image % d. tmp " I)
Let ! Pixels = instream. readasync (numpixels) // Change to asyncread
Let Pixels ' = Transfromimage (pixels, I)
Use outstream = file. openwrite (sprintf " Image % d. Done " I)
Do ! Outstream. writeasync (pixels ' ) // Change to asyncwrite
}

Let Processimagesasync () =
Printfn " Processimagesasync... "
Let Tasks = [ For I In   1 .. Numimages -> Processimageasync (I)]
Async. Run (async. parallel tasks) | > Ignore // Change to runsynchronously.
Printfn " Processimagesasync finished " AnotherCodeYes Code


Let Processimagesrepeats = 20
Let Repeatn pixels =
Pixels | > Array. Map ( Fun B -> B + 1uy)

Let Transformimage (pixels, imagenum) =
Printfn " Transformimage % d " Imagenum
// Frequent CPU computing operations
[ For I In   1 .. Processimagesrepeats -> Repeatn pixels] // The syntax here is pixels |> func. repeatn processimagesrepeats (array. map (fun B-> B + 1uy) due to func. the repeatn has been removed by the F # library, so it can only be replaced by this syntax

Let Processimagesync (I) =
Use instream = file. openread (sprintf " Image % d. tmp " I)
Let Pixels = array. zerocreate numpixels
Let Npixels = instream. Read (pixels, 0 , Numpixels)
Let Pixels ' = Transformimage (pixels, I)
Use outstream = file. openwrite (sprintf " Image % d. Done " I)
Outstream. Write (pixels ' . [0], 0, numpixels)

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.