Skip to content

Queue destroying on stopping #168

Closed
@ventureoo

Description

Destroying the queue on stop or finish happens either when the items in the queue itself becomes 0, or after a timeout. But originally I thought that queue destroying occurs when the queue becomes empty and all workers have finished their work. So the queue is destroyed before the worker with the last element finishes its job. This may be only my problem, but if this is the expected behavior, it would be nice to clarify it in the documentation, as there are a number of cases where we want to expect not only the queue to be empty, but all its workers to complete. E. g.:

local copas = require("copas")

local function main()
    local queue = copas.queue.new()

    for i = 1, 3 do
        queue:push(i)
    end

    queue:add_worker(function (number)
        copas.pause(2)
        print("Queue handler #" .. number .. " done")
    end)
    queue:finish()
    print("Exit from parent thread")
end

copas.addthread(main)
copas()

Result is:

> luajit queue_test.lua
Queue handler #1 done
Queue handler #2 done
Exit from parent thread
Queue handler #3 done

I use the following code to guarantee the behavior when the queue ends with all the workers:

local copas = require("copas")

local function main()
    local queue = copas.queue.new()
    local tasks = 0

    for i = 1, 3 do
        tasks = tasks + 1
        queue:push(i)
    end

    queue:add_worker(function (number)
        copas.pause(2)
        print("Queue handler #" .. number .. " done")
        tasks = tasks - 1
    end)
    queue:finish()

    while tasks > 0 do
        print("Waiting for the handlers to complete...")
        copas.pause(1)
    end
    print("Exit from parent thread")
end

copas.addthread(main)
copas()

Result is:

Queue handler #1 done
Queue handler #2 done
Waiting for the handlers to complete...
Waiting for the handlers to complete...
Queue handler #3 done
Exit from parent thread

I also tried to use queue:get_workers() to check which workers were still working, but that produced the same result as the first one. But maybe you have a better solution. Thanks.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions