Hi, I just started using Nim about 2 month ago, and for one my project I need to send lot of requests to some server using proxies, so I want to use a some threads to do that and also as proxies doesn't work all the time I would like to timeout the requests if they take too long to process (tested the timeout = X but doesn't seems to work).
Thanks for your help
Thread don't have inherent timeout mechanism. You would have to implement one.
But maybe are you looking for asynchrone request ? In which case, you can look into https://nim-lang.org/docs/asyncdispatch.html and https://nim-lang.org/docs/asyncnet.html
If all you need to do is send a lot of HTTP requests at the same time I'd go with async instead of threading, it will likely net you much better performance.
But if you actually want to use threading then you can have a look at the threads module (note that you don't import it directly), and possibly also the channels module. That should give you a decent idea of how to do threading in Nim.
Thanks, I found what I need in term of threading, but can we store a thread as an object in a variable using the spawn proc from treadpool ? I would like to do something like this
import threadpool, time
proc testproc(data: int) =
do_something_long_with_data(data)
let now = time.now
let thread = spawn testproc(50)
while thread.isAlive():
if time.now - now > 200:
kill(thread)
echo "TIMED OUT"
By the way, if you know how to do the same thing with async procedures I accept it too