Created
September 8, 2017 13:51
-
-
Save xmonader/a3b17a74eab8212cfab6cbc55610f786 to your computer and use it in GitHub Desktop.
simplelinkschecker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, httpclient | |
import threadpool, strutils | |
import times | |
import asyncdispatch | |
type | |
# LinkCheckResult = [link: string, state:bool] | |
LinkCheckResult = ref object | |
link: string | |
state: bool | |
proc checkLink(link: string) : LinkCheckResult = | |
var client = newHttpClient() | |
try: | |
return LinkCheckResult(link:link, state:client.get(link).code == Http200) | |
except: | |
return LinkCheckResult(link:link, state:false) | |
proc sequentialLinksChecker(links: seq[string]): void = | |
for index, link in links: | |
let result = checkLink(link) | |
echo result.link, " is ", result.state | |
proc checkLinkParallel(link: string) : LinkCheckResult {.thread.} = | |
var client = newHttpClient() | |
try: | |
return LinkCheckResult(link:link, state:client.get(link).code == Http200) | |
except: | |
return LinkCheckResult(link:link, state:false) | |
proc threadsLinksChecker(links: seq[string]): void = | |
var LinkCheckResults = newSeq[FlowVar[LinkCheckResult]]() | |
for index, link in links: | |
LinkCheckResults.add(spawn checkLinkParallel(link)) | |
for x in LinkCheckResults: | |
let res = ^x | |
echo res.link, " is ", res.state | |
proc checkLinkAsync(link: string): Future[LinkCheckResult] {.async.} = | |
var client = newAsyncHttpClient() | |
let future = client.get(link) | |
yield future | |
if future.failed: | |
# echo link, " failed" | |
return LinkCheckResult(link:link, state:false) | |
else: | |
let resp = future.read() | |
return LinkCheckResult(link:link, state: resp.code == Http200) | |
proc asyncLinksChecker(links: seq[string]) {.async.} = | |
# client.maxRedirects = 0 | |
var futures = newSeq[Future[LinkCheckResult]]() | |
for index, link in links: | |
futures.add(checkLinkAsync(link)) | |
# waitFor -> call async proc from sync proc, await -> call async proc from async proc | |
let done = await all(futures) | |
for x in done: | |
echo x.link, " is ", x.state | |
when isMainModule: | |
var t: float | |
let links = @["http://google.com", "http://yahoo.com", "http://reddit.com", "http://nope.none"] | |
echo "SEQUENTIAL:: " | |
t = epochTime() | |
sequentialLinksChecker(links) | |
echo epochTime()-t | |
t = epochTime() | |
echo "SPAWNING:: " | |
threadsLinksChecker(links) | |
echo epochTime()-t | |
echo "ASYNC:: " | |
t = epochTime() | |
waitFor asyncLinksChecker(links) | |
echo epochTime()-t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment