I have pasted some of my code below. I am using threads.
How can I save the values
getSellerCountry(html)
and sellerId
in a global, thread-safe variable (table), so that I can later when the threads have finished, write the results into a database? Must I use channels?
proc getSellerCountry(html: string): string =
let xml = parseHTML(html)
let element = xml.querySelectorAll("div.a-row.a-spacing-none.indent-left > span")
return element[element.high].innerText.strip
proc getContent(sellerId: string, url: string, ip: string, user: string, i: int, userAgent: string) {.thread.} =
let headers = setHeader("https://www.google.fr", userAgent)
var myProxy = newProxy(ip, user)
var client = newHttpClient(proxy=myProxy, headers=headers)
let r = client.request(url).body
let html = uncompress(r)
try:
echo getSellerCountry(html), sellerId
except Exception as e:
echo e.msg
echo "Error ", sellerId
for sellerId in sellerIds:
let ua = fk.UserAgent()
url = "https://www.amazon.fr/sp?ie=UTF8&seller=" & sellerId & "&ref_=dp_merchant_link"
spawn getContent(sellerId, url, "http://" & ip[i], userPass[i], i, ua.random.to(string))
i += 1
sync()
@Zoom Thanks. I now am using "parallel". Now it works. The return values of a function in a thread can now be saved. Just an example how to use it:
{.experimental: "parallel".}
import httpclient, threadpool
proc fetch(url: string): string =
return getContent(url)
let urls = ["https://example.com", "https://example.org", "https://example.net"]
var res = newSeq[string]()
var tasks = newSeq[FlowVar[string]](urls.len)
parallel:
for i in 0 ..< urls.len:
tasks[i] = spawn fetch(urls[i])
for task in tasks:
res.add(^task)
echo res