def lease(queue_name, owner, count=1, timeout_seconds=60):
    """Leases a work item from a queue, usually the oldest task available.

    Args:
        queue_name: Name of the queue to lease work from.
        owner: Who or what is leasing the task.
        count: Lease up to this many tasks. Return value will never have more
            than this many items present.
        timeout_seconds: Number of seconds to lock the task for before
            allowing another owner to lease it.

    Returns:
        List of dictionaries representing the task that was leased, or
        an empty list if no tasks are available to be leased.
    """
    now = datetime.datetime.utcnow()
    query = (
        WorkQueue.query
        .filter_by(queue_name=queue_name, status=WorkQueue.LIVE)
        .filter(WorkQueue.eta <= now)
        .order_by(WorkQueue.eta)
        .with_lockmode('update')
        .limit(count))

    task_list = query.all()
    if not task_list:
        return None

    next_eta = now + datetime.timedelta(seconds=timeout_seconds)

    for task in task_list:
        task.eta = next_eta
        task.lease_attempts += 1
        task.last_owner = owner
        task.last_lease = now
        task.heartbeat = None
        task.heartbeat_number = 0
        db.session.add(task)

    return [_task_to_dict(task) for task in task_list]
dataset/ETHPy150Open bslatkin/dpxdt/dpxdt/server/work_queue.py/lease/original