Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SergeyStretovich/4d821546bc83c05b0ec8e03aac0b485a to your computer and use it in GitHub Desktop.
Save SergeyStretovich/4d821546bc83c05b0ec8e03aac0b485a to your computer and use it in GitHub Desktop.
An example of parallelism in F#.
open System
open System.IO
open System.Net
open System.Web
open System.Collections.Generic
open System.Text.RegularExpressions
let urls =["https://en.wikipedia.org/wiki/Toomas_Altnurme"; "https://en.wikipedia.org/wiki/J%C3%BCri_Arrak"; "https://en.wikipedia.org/wiki/Alisa_Jakobi";
"https://en.wikipedia.org/wiki/Kalli_Kalde"; "https://en.wikipedia.org/wiki/Liis_Koger"; "https://en.wikipedia.org/wiki/Mari_Kurismaa";
"https://en.wikipedia.org/wiki/Malle_Leis"; "https://en.wikipedia.org/wiki/Olav_Maran"; "https://en.wikipedia.org/wiki/Peeter_Mudist";
"https://en.wikipedia.org/wiki/Navitrolla"; "https://en.wikipedia.org/wiki/Mall_Nukke"; "https://en.wikipedia.org/wiki/Evald_Okas";
"https://en.wikipedia.org/wiki/Enno_Ootsing"; "https://en.wikipedia.org/wiki/Tiit_P%C3%A4%C3%A4suke"; "https://en.wikipedia.org/wiki/Avo_Paistik";
"https://en.wikipedia.org/wiki/Aapo_Pukk"; "https://en.wikipedia.org/wiki/Martin_Saar"; "https://en.wikipedia.org/wiki/Mart_Sander";
"https://en.wikipedia.org/wiki/Erik_Schmidt_(painter)"; "https://en.wikipedia.org/wiki/Eugen_Sterpu"; "https://en.wikipedia.org/wiki/Viive_Sterpu";
"https://en.wikipedia.org/wiki/Olev_Subbi"; "https://en.wikipedia.org/wiki/Helge_Uuetoa"; "https://en.wikipedia.org/wiki/%C3%9Clo_Vilimaa";
"https://en.wikipedia.org/wiki/Aili_Vint"; "https://en.wikipedia.org/wiki/Mare_Vint"; "https://en.wikipedia.org/wiki/Toomas_Vint"]
let basePath = @"C:\FILES\JSON\"
let writeFile (filePath:string, fileContent:string) =
let encoding = System.Text.Encoding.UTF8
let rr = File.OpenWrite(filePath)
use writer = new IO.StreamWriter(rr,encoding)
writer.Write(fileContent)
let fetchUrl(url:string) =
async{
try
ServicePointManager.SecurityProtocol <- SecurityProtocolType.Tls12 ||| SecurityProtocolType.Tls11
use webClient = new WebClient()
let! downloadedJson = webClient.AsyncDownloadString(Uri(url))
return downloadedJson
with error -> return "Error"
}
let writeJson(content:string)=
try
let converted = Regex.Unescape(content)
let mytitle = Guid.NewGuid().ToString()
let newPath = basePath+mytitle+".json"
writeFile(newPath,converted)
with e -> printfn "FAILURE: %s" e.Message
let handleDocument (uri:string)=
let title = uri.Replace("https://en.wikipedia.org/wiki/","")
let newUrl = "https://en.wikipedia.org/w/api.php?action=query&format=json&titles="+title+"&prop=extracts"
newUrl
let myFunc =
let jsUrls = urls |> Seq.take 20 |> Seq.map(fun uri -> handleDocument uri)
let tasks = [for url in jsUrls -> fetchUrl(url) ]
let parallelTasks = Async.Parallel tasks
let str:string[] = Async.RunSynchronously parallelTasks
str |> Array.iter(fun content -> writeJson content)
()
[<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