Skip to content

Instantly share code, notes, and snippets.

@Gooseus
Created October 25, 2017 05:00
Show Gist options
  • Save Gooseus/240f534fb6277ef13fcd48a579b9edae to your computer and use it in GitHub Desktop.
Save Gooseus/240f534fb6277ef13fcd48a579b9edae to your computer and use it in GitHub Desktop.
Testing async dispatch and http requests in Nim
# testing async dispatch in pursuit of an efficient async s3 interface
import os, times, math, httpclient, asyncdispatch
let t0 = epochTime()
proc dt() : float =
round(epochTime() - t0,6)
const aws_url = "http://localhost:1234/"
proc s3_get_bucket_object(aws_key:string,bucket:string,obj:string) : Future[string] {.async.} =
var
client = newAsyncHttpClient()
c_prog = false
# do the HMAC here
client.headers = newHttpHeaders({ "Authorization": "TEST KEY:SIG-"&obj })
client.onProgressChanged = proc(total, progress, speed: BiggestInt) {.async.} =
#echo("S3 download progress: ", progress, " of ", total)
#echo("S3 transfer rate: ", speed div 1000, "kb/s")
c_prog = true
try:
result = await client.getContent(aws_url)
except HttpRequestError:
echo "http error: "
echo getCurrentExceptionMsg()
except:
echo "unknown exception"
echo getCurrentExceptionMsg()
while not c_prog:
poll()
let bucket = "my_bucket"
var files : seq[(string,Future[string])]= @[]
for f in 0..99:
let file_name = "./some_file_" & $(f+1) & ".txt"
files.add((file_name, s3_get_bucket_object("blah", bucket, file_name)))
for file in files:
#echo file
writeFile(file[0], waitFor file[1])
echo "Transfer Complete ", dt()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment