Skip to content

Instantly share code, notes, and snippets.

@Yardanico
Created October 23, 2017 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yardanico/478b39a78fd9baa7ecb78d1dd672f2b7 to your computer and use it in GitHub Desktop.
Save Yardanico/478b39a78fd9baa7ecb78d1dd672f2b7 to your computer and use it in GitHub Desktop.
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