Re: Help with asynchronously downloading file

2016-08-19 Thread hcorion
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...


Re: Help with asynchronously downloading file

2016-08-18 Thread dom96
Sorry. Not AsyncHttpServer, I meant AsyncHttpClient :)


Re: Help with asynchronously downloading file

2016-08-18 Thread hcorion
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](http://forum.nim-lang.org///nim-lang.org/docs/threads.html)

I have yet to try the AsyncHttpServer method, I'll have to try that one next. 


Re: Help with asynchronously downloading file

2016-08-15 Thread dom96
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:

  * Run `httpclient.downloadFile` (or `get`) in a different thread.
  * Use the `AsyncHttpServer` and poll (by calling the `poll` procedure) the 
async dispatcher in between polls to your GUI. IUP should have something to 
allow you to control the event loop.



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!