implement using generator instead of queue