我不认为我正在研究的这一小段代码有任何实际的应用,但我试图将我的头脑包围在异步中并且似乎有点挣扎。假设我想从Yahoo中提取历史股票价格数据,将所有数据保存到单个csv文件中,然后使用批量复制将其加载到SQL Server中。我不太担心将数据加载到SQL Server中,但我想知道如何将数据写入新的csv文件。可以/应该异步完成吗?
据我所知,在抓取历史数据时无法抓住该流的一部分,因此我抓住该流并将其映射到新列表,并在每个项目的前面添加了自动收录器。我不时运行测试时会得到一个没有自动收报机的记录,并且会有一个带有多个代码的记录(例如,“MSFT,YHOO”)。
所以,我的问题是,如何在不引起问题的情况下将此数据转储到单个csv文件中?其次,当我分割数据时,我得到一个空尾项。放弃它的最佳方法是什么?
就像我说的,我不知道这有任何实际应用,但我正在努力学习,所以我希望你能原谅我的无知。感谢您的帮助,我真的很感激。这就是我所拥有的:
open System
open System.IO
open System.Web
open System.Net
let fromDate = new DateTime(2013, 1, 1)
let getTickers =
"MSFT" :: "YHOO" :: []
let getData (ticker : string) =
async {
let url = System.String.Format("http://ichart.finance.yahoo.com/table.csv?s={0}&g=d&ignore=.csv&a={1}&b={2}&c={3}", ticker, fromDate.Month - 1, fromDate.Day, fromDate.Year)
Console.WriteLine(url)
let req = WebRequest.Create(url)
let! rsp = req.AsyncGetResponse()
use stream = rsp.GetResponseStream()
use reader = new StreamReader(stream)
let lines =
reader.ReadToEnd().Split('\n')
|> Seq.skip 1 // skip header
|> Seq.map (fun line-> (String.Format("{0}, {1}", ticker, line.ToString())))
Seq.iter (fun x->printfn "%s" (x.ToString())) lines
()
}
let z =
getTickers
|> List.map getData
|> Async.Parallel
|> Async.RunSynchronously
答案 0 :(得分:2)
open System
open System.IO
open System.Net
let tickers =
[
"MSFT"
"YHOO"
]
let getData (writer: TextWriter) ticker =
async {
let url = sprintf "http://ichart.finance.yahoo.com/table.csv?s=%s&g=d&ignore=.csv" ticker
let req = WebRequest.Create(url)
let! resp = req.GetResponseAsync() |> Async.AwaitTask
use stream = resp.GetResponseStream()
use reader = new StreamReader(stream)
let! lines = reader.ReadToEndAsync() |> Async.AwaitTask
let lines =
lines.Split('\n')
|> Seq.skip 1
|> Seq.filter ((<>) "") //skip empty lines
for line in lines do
do! writer.WriteLineAsync(String.Format("{0}, {1}", ticker, line)).ContinueWith(ignore) |> Async.AwaitTask
}
let writeAllToFile path =
use writer = new StreamWriter(path=path)
tickers
|> Seq.map (getData writer)
|> Async.Parallel
|> Async.RunSynchronously
|> ignore
writeAllToFile @"C:\quotes.csv"