Created
October 23, 2017 12:45
-
-
Save Yardanico/478b39a78fd9baa7ecb78d1dd672f2b7 to your computer and use it in GitHub Desktop.
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 httpclient, locks, json, strutils, sequtils, os | |
var | |
threads: array[3, Thread[int]] | |
chans: array[3, Channel[string]] | |
echoLock: Lock | |
proc echoTrue(chanId: int) {.thread.} = | |
let url = chans[chanId].recv() | |
while true: | |
let a = newHttpClient() | |
let data = a.get(url) | |
chans[chanId].send($data.code) | |
echoLock.acquire() | |
echo "chan $1 sleeping..." % $chanId | |
echoLock.release() | |
sleep(500) | |
proc main = | |
for i, chan in mpairs(chans): | |
chan.open() | |
chan.send("http://httpbin.org/") | |
createThread(threads[i], echoTrue, i) | |
# can we remove this? | |
sleep(500) | |
while true: | |
for i, chan in mpairs(chans): | |
if chan.ready(): continue | |
let (ok, data) = chan.tryRecv() | |
if ok: | |
echoLock.acquire() | |
echo "received data from chan " & $i | |
echo "data is " & data | |
echoLock.release() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment