Hi all! So I'm writing a custom sort of package manager. However it needs to be GUI, and I can't just stop the gui and download the files (taking up to 4-5 mins to download). So, when the user presses a button, it will trigger a procedure that should check if a download has been started, if it hasn't it'll start it. I have have a separate procedure that the GUI library calls (iup) every, say 500 milliseconds, in which I would check if the download is complete and then execute it, if not it will update a progress bar. The progress bar is not super important, but it is handy.
I'm unsure how to start an async download, and then check in a different procedure if it has completed.
Thanks, hcorion
P.S. It only needs to run on Windows.
Hello!
You won't be able to get the progress of the download easily (unless you hack on the stdlib a bit). But as for downloading without blocking your GUI you've got two options I think:
You might run into trouble with the first one (I'm not sure if httpclient is GC safe), but it should be much simpler to try out. Use the threads module to create a new thread or use spawn.
Hope this helps!
Thanks dom96, when I try to download using httpclient.download file I get
Error: 'threadFunc' is not GC-safe as it accesses 'defaultSSLContext' which is a global using GC'ed memory
I've never used threads before, so my scripting may be wrong, here is my attempt at it:
import locks
import httpclient
var L: Lock
proc threadFunc(interval: tuple[url, output: string]) {.thread.} =
acquire(L) # lock stdout
echo interval.url
downloadFile(interval.url, interval.output)
#echo interval.b
release(L)
initLock(L)
var thread: Thread[tuple[url, output: string]]
createThread(thread, threadFunc, ("http://s2.q4cdn.com/242125233/files/doc_downloads/test.pdf", "test.pdf"))
joinThread(thread)
It's roughly based on the example here: http://nim-lang.org/docs/threads.html
I have yet to try the AsyncHttpServer method, I'll have to try that one next.
Alright, So I tried to create a simple AsyncHttpClient, but I was unable to figure out how to use it... This is as far as I got:
import httpclient, asyncdispatch, os
proc main() {.async.} =
var client = newAsyncHttpClient()
echo("HERE!")
var resp = await client.request("https://ca.yahoo.com/?p=us")
echo("THERE!")
#writeFile("test.html", resp.body)
client.close()
var doneTest: bool = false
proc newWaitFor*[T](fut: Future[T]): T =
if (fut.finished == false):
echo("Before poll")
poll()
echo("After poll")
else:
doneTest = true
echo "done!"
fut.read
var i = 0
while doneTest == false:
i += 1
#echo (i)
if i >= 40:
newWaitFor(main())
It never actually completes for some reason...