So my intention here is to have a single producer/consumer run cooperatively on
a single thread. I'm not looking for any fibers/coroutines that have their own
separate stack, but instead would like to utilize await in asyncdispatch module
or the asymmetrical yield from Nim's iterator. Here is what I have come up with:
1) Using two inline iterators: `
iterator producer(): DataType =
var again = true var data: DataType
while again:
data = fetchData() # handle exception / sanitize yield data again =
data.isEmpty()
iterator consumer(data: DataType): ResultType =
var res: ResultType = filter(data) yield res
for data in producer():
for res in consumer(data):
echo res
` With this nesting of inline iterators am I achieving the pause/resume
cooperation or is this mutual recursion resulting in an eventual overflow?
2) Using closure iterators: `
iterator producer(it: iterator(data): ResulType {.closure.}): ResultType =
var again = true var data: DataType var res: ResultType
while again:
data = fetchData() # handle exceptions / sanitize
for i in it(data):
res = i
yield res again = data.isEmpty()
iterator consumer(data: DataType): ResultType {.closure.} =
var res: ResultType = filter(data) yield res
let consume = consumer
for res in producer(consume):
echo res
` Same question as above
3) Using await: ` import asyndispatch
proc consumer(data: DataType): Future[ResultType] {.async.} =
var res = newFututre[ResultType]("consumer") res = filterFuture(data)
result res.read() # handle exceptions
proc producer() {.async.} =
var again = true var data: DataType
while again:
data = fetchFutureData() # handle exceptions / sanitize let res = await
consumer(data) again = data.isEmpty() debugEcho res
waitFor producer() ` Is this correct? If I yield the res in consumer it will
always return nil. I think this construction is always creating and destroying
Futures, maybe I should be using FutureVar.