f# - Best practices to parallelize using async workflow -


Let's say I wanted to scrape a webpage, and to remove some data, I have to write something like this :

  Let getAllHyperlinks (url: string) = async {letq = WebRequest.Create (url) let's go! Rsp = req.GetResponseAsync () Use stream = rsp.GetResponseStream () / rsp depends on reader = new system. Io Stream depends on the stream (stream) // stream! Data = reader.AsyncReadToEnd () // Depends on Reader Return Extraction AllUrls (data)} / Depends on the data   

Come on! tells F # to execute the code in the second thread, then tie the result in one variable, and continue processing. The above sample uses two two statements: one to get an answer, and one to read all data, so it generates at least two threads (please correct me if I am wrong).

Although many threads are generated above the workflow, the sequence of execution is serial because each item depends on the previous item in the workflow.

More than one in the code above! There is no benefit to being .

If not, how would it be necessary to change this code to take advantage of many codes? statement?

The key is that we not sponges to a new thread During the entire course of workflow, 1 or 0 active threads are being used from Threadpool (one exception, before '!', The code runs on user thread, which was an asynchronous run.) " Let's go!" When an async operation is in the sea, and then raises a thread from Threadpool, when the operation returns, one can go to the thread. The performance against the Threadpool is low pressure (and, of course, the key user advantage is a simple programming model - one million times better than all the Kilifu / Andfu / callback items that you otherwise write).

See also


Comments